4

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?

martineau
  • 119,623
  • 25
  • 170
  • 301
Tony Montana
  • 357
  • 2
  • 5
  • 16
  • Possible duplicate of [Json handling in ROBOT](https://stackoverflow.com/questions/35262216/json-handling-in-robot) – A. Kootstra Mar 01 '19 at 12:31

4 Answers4

8

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}
Todor Minakov
  • 19,097
  • 3
  • 55
  • 60
  • I get "No keyword with 'Evaluate ...' found." It seems like everyone who gives an answer about robot framework leaves out the part that would make their answer work. – Maxx Apr 25 '22 at 17:21
  • I'd be very surprised if `Evaluate` is not recognised, it's a built-in keyword, always available for usage. Are you sure you have 2 or more spaces between it & the 1st argument? If it's just one, the whole string will be parsed as the keyword name & will lead to such exception. – Todor Minakov Apr 26 '22 at 04:37
0

Is this all you're needing?

jsonObj = {
    "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
}

ipv6 = jsonObj['interfaces'][0]['ipv6']

print (ipv6)

Output:

{'mode': 'DISABLED', 'addresses': [], 'gateway': ''}
chitown88
  • 27,527
  • 4
  • 30
  • 59
0

In my case I just put this way and works:

${response.json()['data'][1]['b1_YDESCRI']}
-2

I don't know Robot Framework but if you want to manipulate JSON, you can use the built-in lib json.

import json

data = json.loads(response)

ipv4 = data['interfaces'][0]['ipv4']
MrCed
  • 57
  • 2
  • 9