2

I have JSON objects that I want to concatenate into one JSON object.

How do I do that using NewtonSoft's JSON package?

3

1 Answer 1

6

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.

Json.NET 6.0 Release 4

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).

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

1 Comment

What if you want to copy the nulls across too

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.