How to load a json data file into a variable in robot framework?

Vikraant Singh Sanwal picture Vikraant Singh Sanwal · Jul 14, 2014 · Viewed 27.2k times · Source

I Am trying to load a json data file into a variable directly in Robot Framework. Can anyone please elaborate with an e.g. giving the exact syntax as to how to do it? Thanks in advance :)

Answer

Bryan Oakley picture Bryan Oakley · Jul 14, 2014

One way would be to use the Get File keyword from the OperatingSystem library, and then use the built-in Evaluate keyword to convert it to a python object.

For example, consider a file named example.json with the following contents:

{
    "firstname": "Inigo",
    "lastname": "Montoya"
}

You can log the name with something like this:

*** Settings ***
| Library | OperatingSystem

*** Test Cases ***
| Example of how to load JSON
| | # read the raw data
| | ${json}= | Get file | example.json
| | 
| | # convert the data to a python object
| | ${object}= | Evaluate | json.loads('''${json}''') | json
| | 
| | # log the data
| | log | Hello, my name is ${object["firstname"]} ${object["lastname"]} | WARN

Of course, you could also write your own library in python to create a keyword that does the same thing.