Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSON add node to an existing JObject

Tags:

json

c#

json.net

I am trying to add a new node to an existing JSON JObject, but when I add it does not format correctly. It adds quotes around the entire node, and \ are put in place.

Background: I am loading a JSON file, doing some logic then adding a node back in. Figured I can do it like this:

mainJson.Add("NewNode", JsonConvert.SerializeObject(MyObject));
File.WriteAllText("myfile.json", mainJson.ToString());

Problem is that this is the result:

{
"JSONFile": [
  {
    "More": "Nodes",
    "InThe": "File"
  }
],
"Customers": "{\"FirstName\":\"Mike\",\"LastName\":\"Smith\"},{\"FirstName\":\"Jane\",\"LastName\":\"Doe\"}",
}

I know that my JsonConvert.SerializeObject(MyObject) is working if I do this:

string json = JsonConvert.SerializeObject(MyObject);
File.WriteAllText("myfile2.json" json);

The result is this:

[
  {
    "FirstName": "Mike",
    "LastName": "Smith"
  },
  {
    "FirstName": "Jane",
    "LastName": "Doe"
  }
]

What am I missing?

edit: Following @Swagata Prateek comment of;

mainJson.Add("Customers",JObject.FromObject(MyObject));

An unhandled exception of type 'System.ArgumentException' occurred in Newtonsoft.Json.dll

Additional information: Object serialized to Array. JObject instance expected.

I should note that MyObject is actual ObservableCollection if that makes a difference

like image 455
Xaphann Avatar asked Sep 06 '16 14:09

Xaphann


People also ask

How do you add data to an existing JSON object in node JS?

push(newData); To write this new data to our JSON file, we will use fs. writeFile() which takes the JSON file and data to be added as parameters. Note that we will have to first convert the object back into raw format before writing it.

Can not add JValue to JObject?

You are getting this error because you are trying to construct a JObject with a string (which gets converted into a JValue ). A JObject cannot directly contain a JValue , nor another JObject , for that matter; it can only contain JProperties (which can, in turn, contain other JObjects , JArrays or JValues ).

Can a JToken be a JObject?

So you see, a JObject is a JContainer , which is a JToken . Here's the basic rule of thumb: If you know you have an object (denoted by curly braces { and } in JSON), use JObject. If you know you have an array or list (denoted by square brackets [ and ] ), use JArray.


1 Answers

Could you kindly try with this?

mainJson.Add("NewNode", JObject.FromObject(MyObject));
File.WriteAllText("myfile.json", mainJson.ToString());

When you are doing JsonConvert.SerializeObject(MyObject) it serializes MyObject and in the process you get a string out of it.

When you assign mainJson.Add("NewNode", JsonConvert.SerializeObject(MyObject)); you're assigning a string to NewNode. Thus you get a quoted string that represents serialized MyObject

Update:

JArray.FromObject is the method you'd want to look for if you want to convert your collection to a JArray. In that case the segment would look something like

mainJson.Add("NewNode", JArray.FromObject(obsColl));
File.WriteAllText("myfile.json", mainJson.ToString());
like image 114
Swagata Prateek Avatar answered Nov 10 '22 01:11

Swagata Prateek