-1

I am trying to check if two JSON file is equal or not using java :


This is the first json

{
    "filters": [
        {
            "name": "Data_Type",
            "value": "Database"
        },
        {
            "name": "Begin_Date",
            "value": "2019-05-01"
        },
        {
            "name": "End_Date",
            "value": "2019-10-31"
        }
    ]
}

and this is the secound one :

{
    "filters": [
        {
            "name": "End_Date",
            "value": "2019-10-31"
        },
        {
            "name": "Begin_Date",
            "value": "2019-05-01"
        },
        {
            "name": "Data_Type",
            "value": "Database"
        }
    ]
}

I use this library zjsonpatch This library is great but the issue for me I want to ignore the order for the array so In my two JSON file should match Alo I don't need only check the match, also I need report with the defferant if exists as zjsonpatch provide any suggestion ??

  • 1
    Looks like this question is already asked: https://stackoverflow.com/questions/2253750/testing-two-json-objects-for-equality-ignoring-child-order-in-java Looks like the answer there solves your question. – bramdc May 20 '20 at 13:54
  • Thanks, but I need a report with the difference not only equivalent as I mention in the question – Suhaib Abu shawesh May 20 '20 at 14:12

1 Answers1

-1

Your problem is relatively simple to solve. I won't write real code because you didn't specify what language you are using so you will get pseudocode.

    create a method to determine if a json object is equal to another according to your rules

parse json a
place every individual item of json a into a collection

parse json b
place every individual item of json a into another collection

iterate over collection a removing item by item and trying to removing the current item from collection b

when a item from A can't be removed from B or if when you finish iterating collection B is not empty it means the jsons were different
ChrisF
  • 134,786
  • 31
  • 255
  • 325
Rafael Lima
  • 3,079
  • 3
  • 41
  • 105