Uday T
Uday T

Reputation: 137

Parsing JSON output in Robot framework [TypeError: expected string or buffer]

I have a JSON output like this:

{
  "output": {
    "ParentKey": {
      "key": "9b92e663a66c0cc1",
      "id": "uid=26"
    }
  }
}

I'm trying to fetch the content of key, that is: 9b92e663a66c0cc1

Lets say ${output} has this JSON content/data..

This is the code I tried:

${values}=    Evaluate    json.loads($output)    json
${response}    Evaluate      json.loads($values['output']['ParentKey'])  json
${key}  set variable  ${response['ParentKey']['key']}
[Return]  ${key}

I'm using older Robot (less than v3.2)

However, I'm getting an error like this

Evaluating expression 'json .loads (RF_VAR_values ['output']['ParentKey'])' failed: TypeError: expected string or buffer

How do I fetch the key without getting above error?

Thank you!!!

Upvotes: 0

Views: 694

Answers (1)

rasjani
rasjani

Reputation: 7970


*** Settings ***
*** Variables ***

${our_json}   { "output": { "ParentKey": { "key": "9b92e663a66c0cc1", "id": "uid=26" } } }


*** Test Case ***
Test JSON
  ${values}=    Evaluate    json.loads($our_json)    json
  Log To Console    ${values}
  ${key}  set variable  ${values['output']['ParentKey']['key']}
  Log To Console   OUR KEY: ${key}

outputs:

==============================================================================
Test
==============================================================================
Test JSON                                                             .{'output': {'ParentKey': {'key': '9b92e663a66c0cc1', 'id': 'uid=26'}}}
..OUR KEY: 9b92e663a66c0cc1
Test JSON                                                             | PASS |
------------------------------------------------------------------------------
Test                                                                  | PASS |
1 critical test, 1 passed, 0 failed
1 test total, 1 passed, 0 failed
==============================================================================

Upvotes: 2

Related Questions