0

There are 2 Json strings to compare.

1

"values": {
    "-1487778947": {
      "field1": "xxx",
      "field2": "yyy",
      "field3": {
        "zzz": {
          "field4": 21,
          "field5": 28
        }
      }
    },
    "-1820451085": {
      "field1": "fgf",
      "field2": "dfd",
      "field3": {
        "zzz": {
          "field4": 56,
          "field5": 78
        }
      }
    },
}

2

"values": {
    "343434-35454-232467498": { // ignore this value
      "field1": "xxx",  // compare these fields
      "field2": "yyy",  // compare these fields
      "field3": {       // compare these fields
        "zzz": {        // compare these fields
          "field4": 21, // compare these fields
          "field5": 28  // compare these fields
        }
      }
    },
    "486787-4546-787344353": { // ignore this value
      "field1": "fgf",   // compare these fields
      "field2": "dfd",   // compare these fields
      "field3": {        // compare these fields
        "zzz": {         // compare these fields
          "field4": 56,  // compare these fields
          "field5": 78   // compare these fields
        }
      }
    },
}

I want to ignore the key of these objects and just match the inside fields. Is this possible with JsonAssert or any other library? We can ignore the fields using customization. But not found a way to ignore only the object key and validate the child values.

1 Answers1

0

With Jayway-JSONPath you can get only the child nodes from both JSON, thus ignoring the key.

INPUT JSON

{
    "values": {
        "-1487778947": {
            "field1": "xxx",
            "field2": "yyy",
            "field3": {
                "zzz": {
                    "field4": 21,
                    "field5": 28
                }
            }
        },
        "-1820451085": {
            "field1": "fgf",
            "field2": "dfd",
            "field3": {
                "zzz": {
                    "field4": 56,
                    "field5": 78
                }
            }
        }
    }
}

JSONPath

$.values.[*]

Output

[
   {
      "field1" : "xxx",
      "field2" : "yyy",
      "field3" : {
         "zzz" : {
            "field4" : 21,
            "field5" : 28
         }
      }
   },
   {
      "field1" : "fgf",
      "field2" : "dfd",
      "field3" : {
         "zzz" : {
            "field4" : 56,
            "field5" : 78
         }
      }
   }
]

Online Test Tool :

  1. JSONLint - The JSON Validator
  2. Jayway JsonPath Evaluator
Akshay G
  • 2,070
  • 1
  • 15
  • 33