In this article, we are going to learn how to insert a key-value pair into an existing JSON object.
Let’s start.
Use Json.NET to Insert a Key Value Pair Into an Existing JSON
Json.NET is one of the most popular JSON frameworks for .NET and we are going to use it for the examples in this article.
To use Json.NET, we must include the Newtonsoft.Json package in our project:
dotnet add package Newtonsoft.Json
After adding the package, we need to include a required namespace to be able to use it for serializing, deserializing, and manipulating the JSON:
using Newtonsoft.Json
Insert A String Value Using the Add Method
To start, let’s take a look at the JSON object we will be using throughout this article:
var json = @"{ ""FirstName"": ""Audrey"", ""LastName"": ""Spencer"", ""ContactDetails"": { ""Country"": ""Spain"" } }";
Now, we are going to show an example of how we can add a specified property name with a string value using JObject
:
public JObject AddStringValue(string propertyName, string value) { var json = JObject.Parse(_json); json.Add(propertyName, value); return json; }
We declare the AddStringValue
a method that accepts two string
parameters, the name of the property, and a new value. Inside the method, we parse the _json
string and use the Add
method to add the property name and the value for that property.
Now, let’s call this method from the Program class with the previous JSON:
var parser = new Parser(json); var jasonUpdated = parser.AddStringValue("UserName", "Audrey_Spencer72"); Console.WriteLine(jasonUpdated);
First, we instantiate the Parser
class. Then, we invoke the AddStringValue
method to add a new key-value pair to the JSON:
{ "FirstName": "Audrey", "LastName": "Spencer", "ContactDetails": { "Country": "Spain" }, "UserName": "Audrey_Spencer72" }
As we can observe, the last key-value pair (UserName and its value), are added to an existing JSON.
Insert An Object Value Using the Add Method
We can use the Add
method as well if we have an object that we want to add as a value inside the JSON
object:
public JObject AddObjectValue(string propertyName, object value) { var json = JObject.Parse(_json); json.Add(propertyName, JObject.FromObject(value)); return json; }
Here, we accept two parameters, the string propertyName
, and the object value
. Then, inside the method, we parse the _json
string again and then use the Add
method to add the property name and the value for that property. Just this time, we have to use the JObject.FromObject
method instead of just using the value as a parameter.
Now, we can call this method:
var parser = new Parser(json); var jasonUpdated = parser.AddObjectValue("AdditionalDetails", new AdditionalDetails { FullName = "Audrey Spencer", UserName = "Audrey_Spencer72" }); Console.WriteLine(jasonUpdated);
And inspect the result:
{ "FirstName": "Audrey", "LastName": "Spencer", "ContactDetails": { "Country": "Spain" }, "AdditionalDetails": { "FullName": "Audrey Spencer", "UserName": "Audrey_Spencer72" } }
In this case, we add the last key-value pair, AdditionalDetails
and its value, into an existing JSON.
Insert a Key Value Pair Into an Existing JSON Directly
Now, we are going to show an example of how we can add a string value inside JSON
directly:
public JObject InsertStringValue(string existingProperty, string newProperty, string value) { var json = JObject.Parse(_json); json[existingProperty][newProperty] = value; return json; }
Now, we have theInsertStringValue
method that receives three string
parameters. Again, inside the method, we parse the _json
string and access directly to JSON
assignment with the json[existingProperty][newProperty]
expression. With this expression, we can add a new key-value pair to a JSON’s property, which is a JSON itself (the ContactDetails
property in our case).
Again, let’s call the method:
var parser = new Parser(json); var jasonUpdated = parser.InsertStringValue("ContactDetails", "Address", "4455 Landing Lange"); Console.WriteLine(jasonUpdated);
After we run our app, we can inspect the result:
{ "FirstName": "Audrey", "LastName": "Spencer", "ContactDetails": { "Country": "Spain", "Address": "4455 Landing Lange" } }
We can confirm that the new Address
key-value pair has been added.
Insert An Object Value Directly Into an Existing JSON
We have the same option if we want to add an object directly to an existing JSON
:
public JObject InsertObjectValue(string existingProperty, string newProperty, object value) { var json = JObject.Parse(_json); json[existingProperty][newProperty] = JObject.FromObject(value); return json; }
It’s similar to the previous method and we use the FromObject
method to add an object value.
Let’s call this method:
var parser = new Parser(json); var jasonUpdated = parser.InsertObjectValue("ContactDetails", "Address", new Address { Number = 123, Street = "Abc" }); Console.WriteLine(jasonUpdated);
Here, we demonstrate adding a new Address
object by invoking the InsertObjectValue
method.
Finally, we can inspect the result:
{ "FirstName": "Audrey", "LastName": "Spencer", "ContactDetails": { "Country": "Spain", "Address": { "Number": 123, "Street": "Abc" } } }
We can verify the new Address
key-value object.
Conclusion
In this article, we’ve learned different ways to insert a key-value pair into an existing JSON object. We’ve seen how to add both a string value and an object value to a JSON object either directly or by using the Add
method.