1

I'm trying to create this JSON below using JSON.Net but i received an error of

Can not add Newtonsoft.Json.Linq.JProperty to Newtonsoft.Json.Linq.JArray

I was able to get the output up until "cpu" but i can't create "disks" which involves array.

JSON I'm trying to make:

{
  "spec":{
     "name":"SampleVM",
     "cpu":{
        "hot_remove_enabled":true,
        "count":1,
        "hot_add_enabled":true,
        "cores_per_socket":1
     },
     "disks":[
        {
           "new_vmdk":{
              "capacity":1024
           }
        }
     ]
  }
}

My code

JObject newjson =
    new JObject(
        new JProperty("spec", 
            new JProperty("name","SampleVM"),                  
            new JProperty("cpu",new JObject
            { 
            new JProperty("hot_remove_enabled",true),
            new JProperty("count",1),
            new JProperty("hot_add_enabled",true),
            new JProperty("cores_per_socket",1)
            }),

            new JProperty("disks", new JArray(
                new JObject
                {
                    new JProperty("new_vmdk",new JObject{
                        new JProperty("capacity",1024)
                    })
                }
                ))));

What can i change in my code to get the exact same output as the JSON? My problem mainly lies on trying to recreate "disks" JProperty which has array.

1 Answer 1

0

Your problem is here:

new JObject(
    new JProperty("spec", 
        new JProperty("name","SampleVM"),
        new JProperty("cpu", // Remainder omitted.

What you are doing here is adding multiple JProperty objects to a JProperty. A property can't have properties as children, only an object can have properties as children.

You probably want those nested properties encapsulated in an object like so:

var newjson =
    new JObject(
    new JProperty("spec", 
                  new JObject(
                      new JProperty("name","SampleVM"),                  
                      new JProperty("cpu",new JObject
                                    { 
                                        new JProperty("hot_remove_enabled",true),
                                        new JProperty("count",1),
                                        new JProperty("hot_add_enabled",true),
                                        new JProperty("cores_per_socket",1)
                                    }),

                      new JProperty("disks", new JArray(
                          new JObject
                          {
                              new JProperty("new_vmdk",new JObject{
                                  new JProperty("capacity",1024)
                              })
                          }
                      )))));    

Demo fiddle here.

Notes:

  • The misleading error message Can not add Newtonsoft.Json.Linq.JProperty to Newtonsoft.Json.Linq.JArray seems to come from the fact that you are trying to add multiple properties to a property, in which case the JProperty constructor assumes that since a collection is being passed in, a JArray should be made out of the incoming arguments -- and fails, since the arguments are all of type JProperty.

    If I add just one property to a property like so:

    new JObject(
        new JProperty("spec", 
                      new JProperty("name","SampleVM")
                     )
    );
    

    I get a less misleading message Can not add Newtonsoft.Json.Linq.JProperty to Newtonsoft.Json.Linq.JProperty. Demo fiddle #2 here.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.