How to get a required parameter from JSON response string

9,721 views
Skip to first unread message

prasanth kotagiri

unread,
Mar 9, 2016, 7:06:46 AM3/9/16
to robotframework-users
Hi

Following below is the JSON respose i got from post request:

{
  "macKey": "This_is_a_Test_QED_MAC_Key_Which_Needs_to_be_at_Least_32_Bytes_Long",
  "subject": "deli...@token.com",
  "audience": "qed:test",
  "expirationTime": "2016-03-09T23:07:52Z",
  "notBeforeTime": "2016-03-09T23:07:52Z",
  "jwtId": "",
  "permissions": [
    "MANAGE_SYSTEM"
  ],
  "useCompactPermissions": false,
  "generatedToken": "eyJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJ0ZXN0Iiwic3ViIjoiZGVsaXZlcnlAdG9rZW4uY29tIiwiYXVkIjoicWVkOnRlc3QiLCJxZWRwIjpbIm1hbmFnZVN5c3RlbSJdLCJleHAiOjE0NTc1NjQ4NzJ9.8KlDoK-t6HoOth3na2x6drEAFfXCGLKLt3aAMGg0r0U",
  "qeda": null,
  "qedp": [
    "manageSystem"
  ],
  "issueTime": "2016-03-09T09:34:40Z"
}


From the above string i need to take out the value from parameter "generatedToken".

Is there any keyword for this how i need to do.Please help me on this ASAP.

Thanks
Prasanth

Kevin O.

unread,
Mar 9, 2016, 1:45:50 PM3/9/16
to robotframework-users
If you are using the Requests HTTP library and you have the response stored in a variable called ${resp}, you should be able to get the value like this:

${token}=    Collections.Get From Dictionary    ${resp.json()}    generatedToken

Zeke Fiddler

unread,
Mar 11, 2016, 1:02:00 PM3/11/16
to robotframework-users
Hi Prasanth,

I have found that HTTP Library (Liveliest) has a few useful keywords for working with JSON dicts.

A simple line to get the desired value from your json -- let's call it ${your_json} --  would look something like this

| ${generated_token}= | Get Json Value | ${your_json} | /generatedToken |

With this library the scheme for pointing to json values is similar to a directory hierarchy.  So if there were embedded json dicts within your json, you would point to specific values using the "/" as a separator. The example given in the keyword documentation provides a helpful illustration of this 
${result}=Get Json Value{"foo": {"bar": [1,2,3]}}/foo/bar

I leverage the entire library to test our REST API endpoints, but the buried lede here is that there are some simple yet very helpful keywords which greatly simplify working with JSON as well.  The upshot is that your Robot Framework test cases will not require as much esoteric pythonic syntax in order to extract the desired values from your JSON.

I hope this is helpful.

Kindest Regards,

Zeke Fiddler

Andrei Chircu

unread,
Jun 26, 2016, 3:22:03 AM6/26/16
to robotframework-users
Hi, guys I need to use extracted token now but I just can't:

First api
    create http context                   ${url}   https
    Set Request Header                    Content-Type         application/json
    Set Request Body                      ${Login_JSON_DATA}
    POST                                  /api/auth
    ${token}=        get response body
    Response Status Code Should Equal     200
    response body should contain  token
    ${token_2}=    get json value  ${token}     /token
    Set Request Header                    Authorization    Bearer ${token_2}
    Get                                   /api/status
Can you please point on what I'm not doing correct?

Aswathy Krishnan

unread,
Jan 16, 2019, 1:02:56 AM1/16/19
to robotframework-users


On Thursday, 10 March 2016 00:15:50 UTC+5:30, Kevin O. wrote:
If you are using the Requests HTTP library and you have the response stored in a variable called ${resp}, you should be able to get the value like this:

${token}=    Collections.Get From Dictionary    ${resp.json()}    generatedToken


Hi Kevin,
This worked for me, but what if i have nested json? How can i get the value then?
"vegetables": "All veggies"
"fruit": {
        "apples": "Hip Hip,!",
        "oranges": "Hip",
        
    }, 
How can i get the value of apple?

Dmitriy Chernoshey

unread,
Jan 16, 2019, 10:15:58 AM1/16/19
to aswathyk...@gmail.com, robotframework-users
Kevin, I don't think there is Requests HTTP Library. There is Robotframework-requests and there is a separate library named HttpLibrary. 

It has keyword Get Json Value:
$result =     Get Json Value    ${nested_fruit_dictionary}     /fruit/apples

It uses JsonPointer to navigate JSON structures. JsonPointer can handle nested dictionaries with lists, too. All you have to do is provide the position index of your list element. For instance, if your "fruit" was a list of dictionaries and apples were the second in the list, you would write /fruit/1/apple

the only problem I noticed working with it is that it returned strings with quotes around them instead of pure string literal value like when you want to get apple and instead get "apple" . But that was 3 years ago. 


--
You received this message because you are subscribed to the Google Groups "robotframework-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to robotframework-u...@googlegroups.com.
To post to this group, send email to robotframe...@googlegroups.com.
Visit this group at https://groups.google.com/group/robotframework-users.
For more options, visit https://groups.google.com/d/optout.


--

Dmitriy Chernoshey

Senior Automation Engineer

dmitriy.c...@rallyhealth.com

RLLY by Rally Health, Inc.

Reply all
Reply to author
Forward
0 new messages