Merge two Jsons using C# code example

Merge two Jsons using C# code example

Merge

In this article we will see how we can merge two different json strings together using C# Newtonsoft.

Code example

private void btnJtodken_Click(object sender, RibbonControlEventArgs e)
{
	//First json
	JObject json1 = JObject.Parse(@"{
	  'EmployeeName': 'John',
	  'EmployeeCode': 'C001',
	  'Designation': 'Engineer',
	  'Gender': [ 'Male','Female' ]
	}");
	
	//Second Json
	JObject json2 = JObject.Parse(@"{
	  'Department': 'Finance',
	  'JobType': [ 'Contract', 'Permanent' ]
	}");

	//Perform merge
	json1.Merge(json2, new JsonMergeSettings
	{
		// union array values together to avoid duplicates
		MergeArrayHandling = MergeArrayHandling.Union
	});
	
	//Output
	string finalJson = json1.ToString();
}

Output

figure 1.0

Please leave your comments or queries under comment section also please do subscribe to out blogs to keep your self upto date.

Leave a Reply

Your email address will not be published.