Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get value from JSON list within Robot Framework?

As part of verification in Robot Framework, I have following data (stored as ${response}) as get request response:

{
    "interfaces": [
        {
            "name": "eth0",
            "status": "ready",
            "macAddress": "xx:xx:xx:xx:xx:xx",
            "ipv4": {
                "mode": "DHCP",
                "address": "127.0.0.1",
                "mask": "255.255.255.0",
            },
            "ipv6": {
                "mode": "DISABLED",
                "addresses": [],
                "gateway": "",
            }
        }
    ],
    "result": 0
}

And I would like to get value of key ipv4 and compare it with predefined value. I tried to use it out of HttpLibrary.HTTP as this will be deprecated for Robot Framework 3.1 so I would like to use Evaluate. Will it be possible within Robot Framework?

like image 586
Tony Montana Avatar asked Sep 10 '25 16:09

Tony Montana


1 Answers

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}
like image 113
Todor Minakov Avatar answered Sep 13 '25 05:09

Todor Minakov