Add JObject to JObject

37,142

Solution 1

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).

To make it work, change your second line to this:

json.Add(new JProperty(fm.Name, new JObject()));

Working demo: https://dotnetfiddle.net/cjtoJn

Solution 2

One more example

var jArray = new JArray {
    new JObject
    {
        new JProperty("Property1",
            new JObject
            {
                new JProperty("Property1_1", "SomeValue"),
                new JProperty("Property1_2", "SomeValue"),
            }
        ),
        new JProperty("Property2", "SomeValue"),
    }
};

Solution 3

json["report"] = new JObject
    {
        { "name", fm.Name }
    };

Newtonsoft is using more direct-like approach, where You can access any property via square brackets []. You just need to set the JObject, which have to be created based on Newtonsoft specifics.

Full code:

var json = JObject.Parse(@"
{
    ""report"": {},
    ""expense"": {},
    ""invoices"": {},
    ""settings"": {
        ""users"" : {}
    },
}");

Console.WriteLine(json.ToString());

json["report"] = new JObject
    {
        { "name", fm.Name }
    };

Console.WriteLine(json.ToString());

Output:

{
  "report": {},
  "expense": {},
  "invoices": {},
  "settings": {
    "users": {}
  }
}

{
  "report": {
    "name": "SomeValue"
  },
  "expense": {},
  "invoices": {},
  "settings": {
    "users": {}
  }
}

As a reference, You can look at this link: https://www.newtonsoft.com/json/help/html/ModifyJson.htm

Share:
37,142

Related videos on Youtube

DoobieAsDave
Author by

DoobieAsDave

I'm interested in digital / analog music productions as well as musical programming languages like Chuck and SuperCollider

Updated on July 14, 2022

Comments

  • DoobieAsDave
    DoobieAsDave almost 2 years

    I have a json structure like this:

    var json = 
    {
      "report": {},
      "expense": {},
      "invoices": {},
      "projects": {},
      "clients": {},
      "settings": {
        "users": {},
        "companies": {},
        "templates": {},
        "translations": {},
        "license": {},
        "backups": {},
      }
    }

    I would like to add a new empty Object like "report":{} to the json

    My C# code is like this:

    JObject json = JObject.Parse(File.ReadAllText("path"));
    json.Add(new JObject(fm.Name));
    

    But it it gives me a exception: Can not add Newtonsoft.Json.Linq.JValue to Newtonsoft.Json.Linq.JObject

    So, how can I add a new and empty JObject to the json

    Thanks in advance

    • gvmani
      gvmani over 6 years
      JObject doesn't have a constructor which takes JValue param. [newtonsoft.com/json/help/html/…
    • gvmani
      gvmani over 6 years
      json.Add(new JObject().Add("test",new JValue(fm.Name)));