JSONDiff

compares two JSON files and returns their differences, such as added, removed, or changed fields, in a structured format.

python · Common Scripts

Details

IDJSONDiff
Languagepython
From Version6.10.0
Docker Imagedemisto/python3:3.12.13.10404775
TagsUtility

README

Script to Compare JSON Files and Output Differences

This script compares two JSON files and returns their differences, such as added, removed, or changed fields, in a structured format.

Script Data


Name Description
Script Type python3
Cortex XSOAR Version 6.1.0

Inputs


Argument Name Description
old_json The first JSON object to compare.
new_json The second JSON object to compare.

Outputs


Path Description Type
JSONDiff.changed List of fields that have changed between the two JSONs. Array
JSONDiff.added List of fields that were added in the second JSON. Array
JSONDiff.removed List of fields that were removed from the first JSON. Array

Example


Input

```json
{
“old_json”: “{"a": 1, "b": 2}”,
“new_json”: “{"a": 1, "b": 3}”
}

import pytest
from unittest.mock import MagicMock
from JSONDiff import compare_jsons

# Mock demisto
demisto = MagicMock()


def test_compare_jsons_simple():
    """
    When: Comparing two JSON objects with simple key-value pairs.
    Given: json1 = {"a": 1, "b": 2}, json2 = {"a": 1, "c": 3}.
    Then: The result should indicate 'b' was removed and 'c' was added.
    """
    json1 = {"a": 1, "b": 2}
    json2 = {"a": 1, "c": 3}
    result = compare_jsons(json1, json2)
    assert result == {"changed": [], "added": [{"field": "c", "value": 3}], "removed": [{"field": "b", "value": 2}]}


def test_compare_jsons_nested():
    """
    When: Comparing two nested JSON objects.
    Given: json1 = {"a": {"b": 1, "c": 2}}, json2 = {"a": {"b": 1, "d": 3}}.
    Then: The result should indicate 'c' was removed and 'd' was added inside 'a'.
    """
    json1 = {"a": {"b": 1, "c": 2}}
    json2 = {"a": {"b": 1, "d": 3}}
    result = compare_jsons(json1, json2)
    assert result == {"changed": [], "added": [{"field": "a.d", "value": 3}], "removed": [{"field": "a.c", "value": 2}]}


def test_compare_jsons_changed():
    """
    When: Comparing two JSON objects with a changed value.
    Given: json1 = {"a": 1, "b": {"c": 2}}, json2 = {"a": 2, "b": {"c": 2}}.
    Then: The result should indicate that 'a' changed from 1 to 2.
    """
    json1 = {"a": 1, "b": {"c": 2}}
    json2 = {"a": 2, "b": {"c": 2}}
    result = compare_jsons(json1, json2)
    assert result == {"changed": [{"field": "a", "from": 1, "to": 2}], "added": [], "removed": []}


@pytest.mark.parametrize(
    "json1,json2,expected",
    [
        ({"a": [1, 2]}, {"a": [1, 3]}, {"changed": [{"field": "a", "from": [1, 2], "to": [1, 3]}], "added": [], "removed": []}),
        ({"a": None}, {"a": 1}, {"changed": [{"field": "a", "from": None, "to": 1}], "added": [], "removed": []}),
        ({"a": True}, {"a": False}, {"changed": [{"field": "a", "from": True, "to": False}], "added": [], "removed": []}),
    ],
)
def test_compare_jsons_various_types(json1, json2, expected):
    """
    When: Comparing JSON objects with various data types.
    Given: Different JSON structures with lists, None values, and boolean types.
    Then: The function should correctly identify changes.
    """
    assert compare_jsons(json1, json2) == expected