How to Get Value from Json List Within Robot Framework

How to get value from JSON list within Robot Framework?

If the variable ${response} is a response object - vs just a string, the content of the payload - the most straightforward way is to call its json() method, which returns the payload as parsed dictionary:

${the data}=    Evaluate    ${response.json()}

Another way is to parse the payload with json.loads() yourself, passing the .content attribute that stores it (this is pretty much what the .json() does internally):

${the data}=    Evaluate    json.loads(${response.content})    json

And if that variable ${response} is a string, the actual payload, then just pass it to json.loads():

${the data}=    Evaluate    json.loads($response)    json

Now that you have the data as a regular dictionary, do your verifications the normal way:

Should Be Equal    ${the data['interfaces'][0]['ipv4']}    ${your predefined dictionary}

Robot framework get from JSON list

You can convert the JSON data to a python object, and then use robot's extended variable syntax to get at the element. In python the value is referenced as data["_embedded"][0]["attr_name"], which translates directly to robot extended variable syntax of ${data["_embedded"][0]["attr_name"]}

Here is a complete example:

*** Test Cases ***
Example
${json}= catenate SEPARATOR=\n
... {"_embedded":[
... {"attr_name":"attr_value_1"},
... {"attr_name":"attr_name_2"}
... ]
... }
${data}= evaluate json.loads('''${json}''') json
should be equal as strings ${data["_embedded"][0]["attr_name"]} attr_value_1
should be equal as strings ${data["_embedded"][1]["attr_name"]} attr_name_2

Get value from Json list then validate the value appear in list

Get values from JSON returns a list with all matching values, while Dictionary Should Contain Value works with and expects, well, a dictionary object.

Just change the keyword for asserting a value is present with something that works with lists - List Should Contain Value, or just Should Contain:

List Should Contain Value     ${models}     train-again-v1-16k

How to iterate to json object and gets its array using robot framework

You can also do this without the JSONLibrary by just using json.loads via the Evaluate Keyword

*** Variables ***
${MY_JSON} {"addedToList":{"locationList":[{"countryType":"US","LineList":[{"Text":"thequickbrownfoxjumps over the lazy dog"}]}],"bikeList":[{"BikeType":"road","LineList":[{"Text":"text"}]}]},"addedLine":"2"}

*** Test Cases ***
Test For Json
${json} Evaluate json.loads('''${MY_JSON}''') json
FOR ${loc_list} IN @{json['addedToList']['locationList']}
Log countryType: ${loc_list["countryType"]} console=True
END
FOR ${bike_list} IN @{json['addedToList']['bikeList']}
Log BikeType: ${bike_list["BikeType"]} console=True
END

Is there a way to get the particular Values from JSON Array using robot or Python code?

I suppose you can have more elements in checkBy arrays, like so:

response = [
{
"Name":"7122Project",
"checkBy": [
{
"keyId": "NA",
"target": "1232"
}
],
"Enabled": False,
"aceess": "123"
},
{
"Name": "7122Project",
"checkBy": [
{
"keyId": "_GUO6g6S3",
"target": "123"
}
],
"aceess": "11222",
"Enabled": False
},
{
"Name": "7122Project",
"checkBy": [
{
"keyId": "-1lLlZOUy",
"target": "e123"
},
{
"keyId": "test",
"target": "e123"
}
],
"aceess": "123"
}
]

then you can key all keyIds in Python with this code:

def get_key_ids(response):
checkbys = [x["checkBy"] for x in response]
key_ids = []

for check_by in checkbys:
for key_id in check_by:
key_ids.append(key_id["keyId"])
return key_ids

for the example above, it will return: ['NA', '_GUO6g6S3', '-1lLlZOUy', 'test_NA'].

You want to get both ids with NA and without NA, so perhaps you can change the function a bit:

def get_key_ids(response, predicate):
checkbys = [x["checkBy"] for x in response]
key_ids = []

for check_by in checkbys:
for key_id in check_by:
if predicate(key_id["keyId"]):
key_ids.append(key_id["keyId"])
return key_ids

and use it like so:

get_key_ids(response, lambda id: id == "NA")     # ['NA']
get_key_ids(response, lambda id: id != "NA") # ['_GUO6g6S3', '-1lLlZOUy', 'test_NA']
get_key_ids(response, lambda id: "NA" in id) # ['NA', 'test_NA']
get_key_ids(response, lambda id: "NA" not in id) # ['_GUO6g6S3', '-1lLlZOUy']

Now it's just a matter of creating a library and importing it into RF. You can get inspiration in the official documentation.

But I need to check the length get all keyID values and store the values that dose not contain NA

I don't completely understand what you are up to. Do you mean length of keyId strings, like "NA" and its length of 2, or the number of keyIds in the response?

How can I do check length and use for loop using robot framework?

You can use keyword Should Be Equal * from BuiltIn library. Some examples of for loops could be found in the user guide here.

Now you should have all the parts you need to accomplish your task, you can try to put it all together.



Related Topics



Leave a reply



Submit