DeLorean
DeLorean

Reputation: 176

How to print only the distinct part of the json after comparing 2 jsons

I am comparing two jsons i am able to find the differences between the jsons. I want to print only the distinguishing part of two jsons. For example:

json1

{
  "Name": "Temperature & Pressure Measurement",
  "Id": "0x0102",
  "Channels": [
    {
      "Data": [
        {
          "Channel0": [
            {
              "Enable": 0,
              "Unit": "Celsius"
            }
          ],
          "Channel1": [
            {
              "Enable": 0,
              "Unit": "Celsius"
            }
          ],
          "Channel2": [
            {
              "Enable": 0,
              "Unit": "Celsius"
            }
          ]
        }
      ]
    }
  ],
  "Events": [
    {
      "event1": 0,
      "event2": 0
    }
  ],
  "Diagnostics": [
    {
      "diag1": 0,
      "diag2": 0
    }
  ],
  "Functions": [
    {
      "Function-1": 0
    }
  ]
}

json2:

{
  "Name": "Temperature & Pressure Measurement",
  "Id": "0x0102",
  "Channels": [
    {
      "Data": [
        {
          "Channel0": [
            {
              "Enable": 1,
              "Unit": "Celsius"
            }
          ],
          "Channel1": [
            {
              "Enable": 0,
              "Unit": "Kelvin"
            }
          ],
          "Channel2": [
            {
              "Enable": 0,
              "Unit": "Kelvin"
            }
          ]
        }
      ]
    }
  ],
  "Events": [
    {
      "event1": 0,
      "event2": 1
    }
  ],
  "Diagnostics": [
    {
      "diag1": 1,
      "diag2": 0
    }
  ],
  "Functions": [
    {
      "Function-1": 0
    }
  ]
}
-------------------------------------------------------------
------------------- Expected Output -------------------------
-------------------------------------------------------------

{
  "Channels": [
    {
      "Data": [
        {
          "Channel0": [
            {
              "Enable": 1
            }
          ],
          "Channel1": [
            {
              "Unit": "Kelvin"
            }
          ],
          "Channel2": [
            {
              "Unit": "Kelvin"
            }
          ]
        }
      ]
    }
  ],
  "Events": [
    {
      "event2": 1
    }
  ],
  "Diagnostics": [
    {
      "diag1": 1
    }
  ]
}

The below is the code i am trying but unable to achieve the output any help will be appreciated!

import json
from pprint import pprint

import dictdiffer

json1 = json.loads("""
{
  "Name": "Temperature & Pressure Measurement",
  "Id": "0x0102",
  "Channels": [
    {
      "Data": [
        {
          "Channel0": [
            {
              "Enable": 0,
              "Unit": "Celsius"
            }
          ],
          "Channel1": [
            {
              "Enable": 0,
              "Unit": "Celsius"
            }
          ],
          "Channel2": [
            {
              "Enable": 0,
              "Unit": "Celsius"
            }
          ]
        }
      ]
    }
  ],
  "Events": [
    {
      "event1": 0,
      "event2": 0
    }
  ],
  "Diagnostics": [
    {
      "diag1": 0,
      "diag2": 0
    }
  ],
  "Functions": [
    {
      "Function-1": 0
    }
  ]
}
""")


json2 = json.loads("""
{
  "Name": "Temperature & Pressure Measurement",
  "Id": "0x0102",
  "Channels": [
    {
      "Data": [
        {
          "Channel0": [
            {
              "Enable": 1,
              "Unit": "Celsius"
            }
          ],
          "Channel1": [
            {
              "Enable": 0,
              "Unit": "Kelvin"
            }
          ],
          "Channel2": [
            {
              "Enable": 0,
              "Unit": "Kelvin"
            }
          ]
        }
      ]
    }
  ],
  "Events": [
    {
      "event1": 0,
      "event2": 1
    }
  ],
  "Diagnostics": [
    {
      "diag1": 1,
      "diag2": 0
    }
  ],
  "Functions": [
    {
      "Function-1": 0
    }
  ]
}
""")
# from deepdiff import DeepDiff
# x = DeepDiff(json1, json2, ignore_order=True)

result = list(dictdiffer.diff(json1, json2))

print('----------------------')
print('-------  DIFF  -------')
print('----------------------')
print(result)
print('----------------------')
print('----------------------')

removal_list = list()

for i in result:
    removal_list.append([x] for x in i[1])
# print(list(removal_list[0]))
# for i in removal_list[0]:
#     print(json1[i])   //  Error is occurring if uncommented print(json1[i]) TypeError: unhashable type: 'list'
#     del json1[i]


pprint(json1)


Current ouput for the above code is

----------------------
-------  DIFF  -------
----------------------
[('change', ['Channels', 0, 'Data', 0, 'Channel0', 0, 'Enable'], (0, 1)), ('change', ['Channels', 0, 'Data', 0, 'Channel1', 0, 'Unit'], ('Celsius', 'Kelvin')), ('change', ['Channels', 0, 'Data', 0, 'Channel2', 0, 'Unit'], ('Celsius', 'Kelvin')), ('change', ['Events', 0, 'event2'], (0, 1)), ('change', ['Diagnostics', 0, 'diag1'], (0, 1))]
----------------------
----------------------
{'Channels': [{'Data': [{'Channel0': [{'Enable': 0, 'Unit': 'Celsius'}],
                         'Channel1': [{'Enable': 0, 'Unit': 'Celsius'}],
                         'Channel2': [{'Enable': 0, 'Unit': 'Celsius'}]}]}],
 'Diagnostics': [{'diag1': 0, 'diag2': 0}],
 'Events': [{'event1': 0, 'event2': 0}],
 'Functions': [{'Function-1': 0}],
 'Id': '0x0102',
 'Name': 'Temperature & Pressure Measurement'}

Process finished with exit code 0

Upvotes: 1

Views: 64

Answers (1)

user20225597
user20225597

Reputation:

How about:

from jsondiff import diff
diff(json1, json2)

which yields a similar output to the one you request (shows differences, if any, but in alphabetical order):

{'Channels': {0: {'Data': {0: {'Channel0': {0: {'Enable': 1}},
     'Channel1': {0: {'Unit': 'Kelvin'}},
     'Channel2': {0: {'Unit': 'Kelvin'}}}}}},
 'Diagnostics': {0: {'diag1': 1}},
 'Events': {0: {'event2': 1}}}

hope this helps.

Upvotes: 2

Related Questions