How to add property in existing json using System.Text.Json library? How to add property in existing json using System.Text.Json library? json json

How to add property in existing json using System.Text.Json library?


Assuming there may be several properties and you want to add a name only to "TestData" property:

using (MemoryStream memoryStream1 = new MemoryStream()){    using (Utf8JsonWriter utf8JsonWriter1 = new Utf8JsonWriter(memoryStream1))    {        using (JsonDocument jsonDocument = JsonDocument.Parse(json))        {            utf8JsonWriter1.WriteStartObject();            foreach (var element in jsonDocument.RootElement.EnumerateObject())            {                if (element.Name == "TestData")                {                    utf8JsonWriter1.WritePropertyName(element.Name);                    // Staring new object                    utf8JsonWriter1.WriteStartObject();                    // Adding "Name" property                     utf8JsonWriter1.WritePropertyName("Name");                    utf8JsonWriter1.WriteStringValue("John");                    // Copying existing values from "TestData" object                    foreach (var testDataElement in element.Value.EnumerateObject())                    {                        testDataElement.WriteTo(utf8JsonWriter1);                    }                    utf8JsonWriter1.WriteEndObject();                }                else                {                    element.WriteTo(utf8JsonWriter1);                }            }            utf8JsonWriter1.WriteEndObject();        }    }    var resultJson = Encoding.UTF8.GetString(memoryStream1.ToArray());}

Here for each property (except for "TestData" property) I write the whole value as is (by calling element.WriteTo(utf8JsonWriter1)), and for "TestData" property I start a new object, add "Name" property and then copy each of the "TestData" object's properties.

P.S. This works, but I'm pretty sure a much better solution should exist.


Here is a possible answer

static void Main(string[] args)    {        var jsonString = @"        {            ""TestData"":{                ""Year__of__Account"":""2019"",                ""Tax___x0025_"":""0.06"",                ""Buildings__1"":""1000"",                ""Contents__1"":""400"",                ""Total_Insurable_Value"":""100"",                ""Buildings__Prem"":""2560.8"",                ""Contents__Prem"":""1707.2"",                ""YB__1"":""1950"",                ""No__Buildings"":""55"",                ""Location_Sprinklers_YN"":""No"",                ""test"":""test""            }        }        ";        var jsonDoc = JsonSerializer.Deserialize<Dictionary<string, object>>(jsonString);        var testDataDict = JsonSerializer.Deserialize<Dictionary<string, object>>(jsonDoc["TestData"].ToString());        testDataDict.Add("Name", "John");        //replace the test data with the modified test data        jsonDoc["TestData"] = testDataDict;        Console.WriteLine(JsonSerializer.Serialize(jsonDoc));    }


I've just created a NuGet package with some hopefully useful extension methods for JsonElement, which allow properties to be added and/or removed. It's based on using the Utf8JsonWriter to create a new mutated JsonElement based on the original, like the answer above.

GitHub repo | NuGet package

var jsonString = "{ \"Name\": \"Andrew\", \"EmailAddress\": \"a@b.com\" }";var jElement = JsonDocument.Parse(jsonString).RootElement;jElement = jElementAddProperty("Age", 38).AddProperty("Male", true).AddProperty("Female", false).AddNullProperty("Alien").AddProperty("Roles", new string[] { "admin", "user" }).AddProperty("LastUpdated", DateTime.UtcNow).AddProperty("crazyNewObject", new{    Name = "Hobbies",    Value = "bass guitar and writing c# code"});

Hopefully someone will find them useful, but if they don't do quite what you need, please enhance and submit a pull request.