user1428019 user1428019 - 2 months ago 10
C# Question

How to append a node in the existing JSON string?

I have a JSON String as below.

{
"Filename":"mypage.html",
"Info":{
"title":{
"Name":"title",
"Values":[
"This is title"
],
"NumericValues":[

],
"DateTimeValues":[

],
"LinkedComponentValues":[

],
"FieldType":0
}
},
"Id":"123",
"Title":"This is my page"
}


I am desearilizing the JSON input string as below.

var outputobject= JsonConvert.DeserializeObject(input);


Now I would like to add a below new node in the input JSON string (after the title in the info) using C#.

"description":{
"Name":"description",
"Values":[
"This is description"
],
"NumericValues":[

],
"DateTimeValues":[

],
"LinkedComponentValues":[

],
"FieldType":0
}


After adding the node, I want to searilize the object into string.
I am facing issue to add the new node in the input json string under the Info node. I am just giving small example. But my input JSON string is big one and it has different structure. but it is valid JSON.

My Final JSON should be as below.

{
"Filename":"mypage.html",
"Info":{
"title":{
"Name":"title",
"Values":[
"This is title"
],
"NumericValues":[

],
"DateTimeValues":[

],
"LinkedComponentValues":[

],
"FieldType":0
}

"description":{
"Name":"description",
"Values":[
"This is description"
],
"NumericValues":[

],
"DateTimeValues":[

],
"LinkedComponentValues":[

],
"FieldType":0
}

},



"Id":"123",
"Title":"This is my page"
}


This is what am trying, I need to add inside the "Info". Please note, am not creating a model or generating input JSON. I am getting an input JSON as string. After getting JSON string, I need to append a attribute (Description) inside the "Info".

var inputobject = JsonConvert.DeserializeObject(input);
var description = new Description
{
Name = "Description",
Values = new List<string>{"This is description"},
NumericValues = new List<string>(),
DateTimeValues = new List<string>(),
LinkedComponentValues = new List<string>(),
FieldType = 0,
KeywordValues = new List<string>(),

};

var descriptionObject = JObject.FromObject(description);
var test = JObject.FromObject(inputobject);
test.Add("Descrition", descriptionObject);


Regards,
Jey

Win Win
Answer

You can use Json.Net's index[] to modify JSON.

enter image description here

class Program
{
    static void Main(string[] args)
    {
        var item = @"
            {
                ""Filename"":""mypage.html"",
                ""Info"":{
                    ""title"":{
                        ""Name"":""title"",
                        ""Values"":[""This is title""],
                        ""NumericValues"":[],
                        ""DateTimeValues"":[],
                        ""LinkedComponentValues"":[],
                        ""FieldType"":0
                    }
                },
                ""Id"":""123"",
                ""Title"":""This is my page""
            }";

        var description = @"{
                ""Name"":""description"",
                ""Values"":[""This is description""],
                ""NumericValues"":[],
                ""DateTimeValues"":[],
                ""LinkedComponentValues"":[],
                ""FieldType"":0}";

        var itemJObj = JObject.Parse(item);
        var descriptionJObj = JObject.Parse(description);

        var titleJObj = itemJObj["Info"] as JObject;
        titleJObj.Add("description", descriptionJObj);

        var serializer = new JsonSerializer{ContractResolver = new CamelCasePropertyNamesContractResolver()};
        var json = JObject.FromObject(itemJObj, serializer);

        Console.WriteLine(json);
        Console.ReadLine();
    }
}
Comments