1

Let take 2 arrays first:

[ {"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},]} ]

Is that possible using json.net ? I know that there is MergeArrayHandling option which can be set to MergeArrayHandling.Merge which in description is defined as "Merge array items together, matched by index." However I didn't find any example of it.

Whenever I am trying to merge them the result is simply replacing the old value with new one.

JArray o1 = JArray.Parse(@"first array")
JArray o2 = JArray.Parse(@"second array")

o1.Merge(o2, new JsonMergeSettings
{
MergeArrayHandling = MergeArrayHandling.Merge
});

string json = o1.ToString();

2 Answers 2

1

The merge option works based on the array index as it says, So for instance if you have an array [1,2,3] and [4] and choose the MergeArrayHandling.Merge Option you will get the result [4,2,3] because both share an index 0 so the value from the incoming is taken over the source. If no index is shared then the source will remain which explains the 2,3 in the result array.

To answer the your specific question yes and no the result you want resembles what would be achieved via a union or concat option, however those don't work because they don't recurse through the objects like merge does, The only way to achieve this would be to break down the Json yourself and do separate merges which is a bit more exhaustive but technically doable.

Sign up to request clarification or add additional context in comments.

Comments

0

You are doing everything correctly, no need for any custom logic. As you said what you need to do is to specify an index and this is quite easy: Let's say you have this:

{
   "abc": "123",
   "testArray":
   [
      { "One": 1 },
      { "Two": 2 },
      { "Three" 3 }
   ]
}

And you want to update the second element from { "Two": 2 } to { "Ten": 10 } all you have to do is to provide your second array like this:

{
   "testArray":
   [
      {  },
      { "Ten": 10 }
   ]
}

To conclude: for merge operations you need to provide a full path (that's why there's testArray in update request) and specify the array index by adding as many empty {} as many indexes you need to skip.

This means that your update json should look like this:

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

Comments

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.