0

I have two JSON array with inner json array

[
   {
      "name":"a",
      "innerArray":[
         {
            "first":1,
            "second":1
         },
         {
            "first":2,
            "second":2
         }
      ]
   },
   {
      "name":"b",
      "innerArray":[
         {
            "first":1,
            "second":1
         },
         {
            "first":2,
            "second":2
         }
      ]
   }
]

second

[
   {
      "name":"a",
      "innerArray":[
         {
            "first":1,
            "second":11
         },
         {
            "first":3,
            "second":22
         }
      ]
   },
   {
      "name":"c",
      "innerArray":[
         {
            "first":1,
            "second":1
         },
         {
            "first":2,
            "second":2
         }
      ]
   }
]

I want to merge them having the output like this

[
   {
      "name":"a",
      "innerArray":[
         {
            "first":1,
            "second":11
         },
         {
            "first":2,
            "second":2
         },
         {
            "first":3,
            "second":22
         }
      ]
   },
   {
      "name":"b",
      "innerArray":[
         {
            "first":1,
            "second":1
         },
         {
            "first":2,
            "second":2
         }
      ]
   },
   {
      "name":"c",
      "innerArray":[
         {
            "first":1,
            "second":1
         },
         {
            "first":2,
            "second":2
         }
      ]
   }
]

I tried parsing it to a JArray and then apply Merge but Whenever I am trying to merge them the result is simply replacing the old value with new one.

 jsonarr1.Merge(jasonarr2, new JsonMergeSettings{
                MergeArrayHandling = MergeArrayHandling.Union,
            });

But I want to replace same key based items and add missing values from second array in above example please see array with name a, its innerArray have replace value of Key 1 and also added new key 3 where Key 2 is intact

5
  • 1
    Does Json.Net PopulateObject - update list elements based on ID answer your question? You would need to make a c# data model corresponding to your JSON to make that answer work for you. Commented Nov 3, 2022 at 4:47
  • Thank you @dbc let me review and confirm Commented Nov 3, 2022 at 15:24
  • 1
    Please share with us that code as well which does not produce the expected output. Commented Nov 4, 2022 at 8:43
  • Added above which I was trying using merge Commented Nov 4, 2022 at 14:45
  • Is this, by any chance, to configure Ocelot and make per-environment overrides? If yes, I started a few days ago a nuget package that re-arranges the Ocelot route configuration to a version that is overridable. My approach was different to yours, though. In any case, it is wj.Ocelot.Configuration. If your case is not Ocelot, this won't help, as I did not make any attempt at logic to override individual values of individual objects at the array element level. Commented Nov 4, 2022 at 15:14

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.