1

I'm not sure of the correct terminology as not overly knowledgeable about json. I have an existing json string/object of the following fixed format (i.e. all items are at the root level and of straightforward key/value pairs, although the number of items can vary):

{
  "product_id": "1777",
  "license_key": "ECHOES-SILENCE-PATIENCE-AND-GRACE",
  "valid_for": "365",
  "status": "active",
  "times_activated_max": 1
}

I need a way to add a json string of varying schema/format that is passed into a method. This object can be of any json format. It's to be added to the root. For example adding this:

{
"name":"John",
"age":30,
"cars":["Ford", "BMW", "Fiat"]
}

..will become this:

{
  "product_id": "1777",
  "license_key": "ECHOES-SILENCE-PATIENCE-AND-GRACE",
  "valid_for": "365",
  "status": "active",
  "times_activated_max": 1
  "name":"John",
  "age":30,
  "cars":["Ford", "BMW", "Fiat"]
}

I have Newtonsoft.Json at my disposal for this.

I did read this but couldn't map it to my problem.

Hope someone can help

stigzler
  • 793
  • 2
  • 12
  • 29
  • Why not? That's the actual answer. What you posted is two *objects*. You're asking how to merge the properties of two objects into a new one. A JSON document may be text, but what it contains are arrays and objects. "Merging" means finding a way to combine those objects/arrays and produce a new array or object. – Panagiotis Kanavos Jun 17 '21 at 12:31
  • 3
    How about reading [documentation](https://www.newtonsoft.com/json/help/html/MergeJson.htm)? – Oliver Jun 17 '21 at 12:31
  • Looks like a duplicate of [Merge two JTokens into one](https://stackoverflow.com/q/37756656/3744182) and/or [How can I merge two JObject?](https://stackoverflow.com/q/21160337/3744182), agree? – dbc Jun 17 '21 at 13:31

2 Answers2

5

The JSON.NET documentation has an article just for that: Merging JSON. You can use JObject.Merge to merge two different objects into one:

JObject o1 = JObject.Parse(@"{
  'FirstName': 'John',
  'LastName': 'Smith',
  'Enabled': false,
  'Roles': [ 'User' ]
}");
JObject o2 = JObject.Parse(@"{
  'Enabled': true,
  'Roles': [ 'User', 'Admin' ]
}");

o1.Merge(o2, new JsonMergeSettings
{
    // union array values together to avoid duplicates
    MergeArrayHandling = MergeArrayHandling.Union
});

What you posted is two objects. A JSON document may be text, but what it contains are arrays and objects. "Merging" means finding a way to combine those objects/arrays and produce a new array or object.

Merge is defined by JContainer, the parent of both JObject and JArray. This means you can use Merge to merge both arrays and objects.

Another option with arrays is to use Enumerable.Union to combine the contents of both arrays, and create a new one :

var array1= JArray.Parse("[1,2,3]");
var array2= JArray.Parse("[3,4,5, \"a\"]");

var array3=new JArray(array1.Union(array2));

This returns [1,2,3,4,5,"a"]

Panagiotis Kanavos
  • 120,703
  • 13
  • 188
  • 236
  • Is there anyway to write our own merger compare function? We have a list of complex objects we are merging, but we only care about the `Id' of them. Not the index position? – Zapnologica Mar 09 '22 at 06:39
2

Json.net uses the JObject type to represent arbitrary JSON objects. This has a handy Merge method for combining two JObjects.

Simply:

string s1 = @"
{
  ""product_id"": ""1777"",
  ""license_key"": ""ECHOES-SILENCE-PATIENCE-AND-GRACE"",
  ""valid_for"": ""365"",
  ""status"": ""active"",
  ""times_activated_max"": 1
}";
string s2 = @"
{
  ""name"":""John"",
  ""age"":30,
  ""cars"":[""Ford"", ""BMW"", ""Fiat""]
}";

JObject o1 = JObject.Parse(s1);
JObject o2 = JObject.Parse(s2);

o1.Merge(o2);

Console.WriteLine(o1);

See it here

canton7
  • 37,633
  • 3
  • 64
  • 77