0

I would like to compare two JSON Objects and return another object with only the properties missing in one JSON Object. For example:

json1 = {
   "name": "ABCD",
   "hobbies": [
     {
       "water_hobby1": "Rafting",
       "water_hobby2": "swimming",
     },
     {
       "hobby3": "Singing",
       "hobby4": "Drawing",
     }
    ]
}

json2 = {
   "name": "ABCD",
   "hobbies": [
     {
       "hobby3": "Singing",
       "hobby4": "Drawing",
     }
    ]
}

Expected output:

json = {
  "hobbies": [
     {
           "water_hobby1": "Rafting",
           "water_hobby2": "swimming",
         },
   ]
}

Lets say I write json1.getDiff(json2); I want the resulting json to contain only the properties that are missing in json2 and present in json1. The nesting of properties and properties themselves is dynamic.

My actual JSOn is like below:

{
  "property1": 0,
  "property2": "value1",
  "property3": [
    {
      "name": "value2",
      "property4": [
        {
          "property5": 0,
          "name": "value3",
          "porperty6": 0,
          "property7": [
            {
              "name": "",
              "property": 0,
              "property": value,
              "property": "value",
              "property": "value",
              "property": "value"
            },
            {
              "name": "",
              "property": 0,
              "property": value,
              "property": "value",
              "property": "value",
              "property": "value"
            },
            {
              "name": "",
              "property": 0,
              "property": value,
              "property": "value",
              "property": "value",
              "property": "value"
            },
            {
              "name": "",
              "property": 0,
              "property": value,
              "property": "value",
              "property": "value",
              "property": "value"
            },
            {
              "name": "",
              "property": 0,
              "property": value,
              "property": "value",
              "property": "value",
              "property": "value"
            },
            {
              "name": "",
              "property": 0,
              "property": value,
              "property": "value",
              "property": "value",
              "property": "value"
            },
            {
              "name": "",
              "property": 0,
              "property": value,
              "property": "value",
              "property": "value",
              "property": "value"
            },
            {
              "name": "",
              "property": 0,
              "property": value,
              "property": "value",
              "property": "value",
              "property": "value"
            }
          ]
        },
  {
   Many Such Objects With nested array and objects
  }]
 },
 {
  "name": "value",
        "property4": []
 }
}
 

Suppose my source JSON Contains Property7[0] Object but my another JSON object does not contain Property7[0] object. I want to only return Property7[0] as my output. The code I am following returns the whole Property7 array which obviously does not contain property7[0] object. But that is not I want. The code is not looping through the objects in the array Property7. some help for me to achieve this is what I seek.

Abhilash D K
  • 1,223
  • 1
  • 23
  • 39
  • I tried the solution presented in `https://stackoverflow.com/questions/24876082/find-and-return-json-differences-using-newtonsoft-in-c` but this returns json1 with water_hobby object removed. The json I am using is much more complecated than the example I gave and I am unable to figure out how to return only the missing objects. – Abhilash D K Jun 03 '19 at 12:12

1 Answers1

0

1st you have to define for yourself this "diff". E.g.: What should be returned when both objects have some property, but have a different value?

Shouldn't be very difficult to write some code based on your definition that just compares the properties of two objects. I think this post can help you: Find and return JSON differences using newtonsoft in C#?

Ronald Korze
  • 734
  • 5
  • 16
  • I am working on the solution present there. The output I am getting is `hobbies - { "+": [ { "hobby3": "Singing", "hobby4": "Drawing" } ], "-": [ { "water_hobby1": "Rafting", "water_hobby2": "swimming" }, { "hobby3": "Singing", "hobby4": "Drawing" } ] }` But I want only water_hobby json to be recorded. I am seeking help to understand how to achieve that. The scenario where the control enters `case JTokenType.Array:` is what I did not understand. – Abhilash D K Jun 03 '19 at 12:43
  • I would like to understand how to again check the arrays for nested objects and perform a recursive call again. – Abhilash D K Jun 03 '19 at 12:47