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

**URL:** <https://forum.robotframework.org/t/is-there-a-way-to-compare-two-json-files-json-body/8859>\
**Category:** JSONLibrary\
**Created:** [13 May 2025 10:38 UTC](https://forum.robotframework.org/t/is-there-a-way-to-compare-two-json-files-json-body/8859 "2025-05-13T10:38:51Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![rofan](https://avatars.discourse-cdn.com/v4/letter/r/58956e/32.png) [@rofan](https://forum.robotframework.org/u/rofan)\
**Post date:** [13 May 2025 10:38 UTC](https://forum.robotframework.org/t/is-there-a-way-to-compare-two-json-files-json-body/8859/1 "2025-05-13T10:38:52Z")

</div>

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](http://jsonplaceholder.typicode.com/todos/1)

If anyone has a suggestion, please 🙂

---

<div class="post-metadata">

**Author:** ![damies13](https://dub1.discourse-cdn.com/flex005/user_avatar/forum.robotframework.org/damies13/32/623_2.png) [@damies13](https://forum.robotframework.org/u/damies13)\
**Post date:** [13 May 2025 16:22 UTC](https://forum.robotframework.org/t/is-there-a-way-to-compare-two-json-files-json-body/8859/2 "2025-05-13T16:22:33Z")

</div>

Hi @rofan,

A JSON file can easily be read and converted to a python dictionary and [Collections](https://robotframework.org/robotframework/latest/libraries/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](https://robotframework.org/robotframework/latest/libraries/BuiltIn.html#Evaluate) to call a python module directly, combine it with [Get File](https://robotframework.org/robotframework/latest/libraries/OperatingSystem.html#Get%20File) and you could do something like this:

```python
${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.

---

<div class="post-metadata">

**Author:** ![laiyee-asm](https://dub1.discourse-cdn.com/flex005/user_avatar/forum.robotframework.org/laiyee-asm/32/4750_2.png) [@laiyee-asm](https://forum.robotframework.org/u/laiyee-asm)\
**Post date:** [15 May 2025 03:31 UTC](https://forum.robotframework.org/t/is-there-a-way-to-compare-two-json-files-json-body/8859/3 "2025-05-15T03:31:09Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![robinmackaij](https://dub1.discourse-cdn.com/flex005/user_avatar/forum.robotframework.org/robinmackaij/32/126_2.png) [@robinmackaij](https://forum.robotframework.org/u/robinmackaij)\
**Post date:** [18 May 2025 12:30 UTC](https://forum.robotframework.org/t/is-there-a-way-to-compare-two-json-files-json-body/8859/4 "2025-05-18T12:30:03Z")

</div>

“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](https://pypi.org/project/deepdiff/) 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).

---

<div class="post-metadata">

**Author:** ![rofan](https://avatars.discourse-cdn.com/v4/letter/r/58956e/32.png) [@rofan](https://forum.robotframework.org/u/rofan)\
**Post date:** [18 May 2025 17:21 UTC](https://forum.robotframework.org/t/is-there-a-way-to-compare-two-json-files-json-body/8859/5 "2025-05-18T17:21:34Z")

</div>

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}

```

---

<div class="post-metadata">

**Author:** ![robinmackaij](https://dub1.discourse-cdn.com/flex005/user_avatar/forum.robotframework.org/robinmackaij/32/126_2.png) [@robinmackaij](https://forum.robotframework.org/u/robinmackaij)\
**Post date:** [20 May 2025 20:28 UTC](https://forum.robotframework.org/t/is-there-a-way-to-compare-two-json-files-json-body/8859/6 "2025-05-20T20:28:27Z")

</div>

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

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

```

If not Evaluate could also be used:

```python
${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.
