Is there a way to compare two JSON files/json body?

I have so far failed to do such a thing - compare entire json files.
Tried with RequestsLibrary, JSONlibrary, Collections working in unison, there is a always some error…
It can be a simple one like on jsonplaceholder.typicode.com/todos/1

If anyone has a suggestion, please :slight_smile:

Hi @rofan,

A JSON file can easily be read and converted to a python dictionary and Collections Library has keywords for comparing dictionaries.

I’m not sure if there’s a json library for robot framework, but you can use Evaluate to call a python module directly, combine it with Get File and you could do something like this:

${jsondata1}=    Get File    path/to/jsonfile1.json
&{jsondict1}=     Evaluate    json.loads('${jsondata1}')    modules=json

${jsondata2}=    Get File    path/to/jsonfile2.json
&{jsondict2}=     Evaluate    json.loads('${jsondata2}')    modules=json

Dictionaries Should Be Equal    ${jsondict1}    ${jsondict2}

Note: I didn’t test this code, it’s a guess by following the documentation but should be close enough to get you close.

Hope that helps,

Dave.

1 Like

Hi @rofan,

My experience is build my own custom keyword by importing the deepdiff.
Here is an example, DeepDiff(json_1, json_2, ignore_order=True)

Hope this help you.
Thanks.

3 Likes

“There’s always some error” is rather vague. What error are you getting in what exact scenario?

Building a custom Python library for this is something I’ve done in the past since it makes loading the json files straighforward (pass the file paths and have the keyword in the library perform a json.load(<file>) and using the DeepDiff module from deepdiff · PyPI to perform the comparison as desired.

Note that “as desired” is rather important here since Python dictionaries / JSON objects are unsorted collections (so the order of the keys does not have to be the same for them to be considered equal) but Python lists / JSON arrays are ordered (so the order of the elements have to be the same to be considered equal).

3 Likes

Thank you all for the input. Will test you solutions!
In the meantime I found a basic working way - not too elegant, but did the job. Pasting test example below.

*** Settings ***
Library           RequestsLibrary
Library           JSONLibrary

*** Comments ***
NOTE: Escape JSON variable content ${EXPECTED_JSON} with any website

*** Variables ***
${BASE_URL}       https://jsonplaceholder.typicode.com
# ${EXPECTED_JSON}  {"userId": 1, "id": 1, "title": "sunt aut facere repellat provident occaecati excepturi optio reprehenderit", "body": "quia et suscipit\\nsuscipit recusandae consequuntur expedita et cum\\nreprehenderit molestiae ut ut quas totam\\nnostrum rerum est autem sunt rem eveniet architecto"}

*** Test Cases ***
Verify Entire JSON Body
    Create Session    myapi    ${BASE_URL}
    ${response}=      GET On Session    myapi    /posts/1

    ${actual_json}=    Convert String To Json    ${response.text}
    ${expected_json}=  Convert String To Json    ${EXPECTED_JSON}

    Should Be Equal    ${actual_json}    ${expected_json}
1 Like

Since the ${response} is just a Python requests Response, you don’t have to use the JSONLibrary to convert it, this should probably work:

VAR    ${actual_json}    ${response.json()}

If not Evaluate could also be used:

${actual_json}    Evaluate    ${response.json()}

If you define the ${EXPECTED_JSON} as a “normal RF dict”, there’s also no need to convert to JSON.

2 Likes