I have JSON objects that I want to concatenate into one JSON object.
How do I do that using NewtonSoft's JSON package?
I have JSON objects that I want to concatenate into one JSON object.
How do I do that using NewtonSoft's JSON package?
Use JContainer.Merge()
.
The logic for combining JSON objects together is fairly simple: name/values are copied across, skipping nulls if the existing property already has a value.
Example:
var jObject1 = // Your first json object as JObject
var jObject2 = // Your second json object as JObject
jObject1.Merge(jObject2);
// jObject1 contains now the merged properties from jObject2.
Note that for properties that exist in both objects, the jObject2
ones take precedence (i.e. overwrite the properties in jObject1
).