Mick
Mick

Reputation: 727

C# JSON Anonymous Type - Multiple properties with the same name

I am needing to produce this JSON string with C#:

{
  "in0": {
    "children": [
      {
        "ValueObjectDescriptor": {
          "fields": [
            {
              "FieldDescriptor": {
                "name": "length",
                "xpath": "@lenth"
              }
            },
            {
              "FieldDescriptor": {
                "name": "height",
                "xpath": "@height"
              }
            },
            {
              "FieldDescriptor": {
                "name": "width",
                "xpath": "@width"
              }
            }
          ],
          "objectName": "Job",
          "limit": 1,
          "xpathFilter": "@openJob = 'true'"
        }
      }
    ]
  }
}

Here is my code:

static string BuildJsonString()
        {
            var json = new
            {
                in0 = new
                {
                    children = new
                    {
                        ValueObjectDescriptor = new
                        {
                            fields = new
                            {
                                FieldDescriptor = new
                                {
                                    name = "length",
                                    xpath = "@length",
                                },
                                FieldDescriptor = new
                                {
                                    name = "height",
                                    xpath = "@height",
                                },
                                FieldDescriptor3 = new
                                {
                                    name = "width",
                                    xpath = "@width",
                                },
                                objectName = "Job",
                                limit = "1",
                                xpathFilter = "@openJob='true'"
                            }
                        }
                    }
                }
            };

            var jsonFormatted = JsonConvert.SerializeObject(json, Newtonsoft.Json.Formatting.Indented);

            return jsonFormatted.ToString();

The issue I am having is that the compiler doesn't like me using "FieldDescriptor" multiple times, I get the error "An anonymous type cannot have multiple properties with the same name".

I am very new to JSON, so any advice would be greatly appreciated.

Upvotes: 0

Views: 1125

Answers (2)

Sweeper
Sweeper

Reputation: 272025

Note that not only is having multiple fields with the same name invalid C# code, having duplicate keys in the same object is also invalid JSON. You can be sure that there is no JSON that would need you to write duplicate field names to serialise it.

The "FieldDescriptor" JSON keys are actually part of different JSON objects, and these JSON objects are all in a JSON array under the key "fields":

[
  {
    "FieldDescriptor": {
      "name": "length",
      "xpath": "@lenth"
    }
  },
  {
    "FieldDescriptor": {
      "name": "height",
      "xpath": "@height"
    }
  },
  {
    "FieldDescriptor": {
      "name": "width",
      "xpath": "@width"
    }
  }
]

The [ ... ] denotes the array, and each pair of { ... } denotes a JSON object. So you should create an (implicitly typed) array of anonymous objects, each one with the FieldDescriptor property, rather than one object having all three of the proeperties:

fields = new[] // <--- create an array
{
   new { 
   FieldDescriptor = new
   {
       name = "length",
       xpath = "@length",
   }},
   new { // notice the new pairs of curly braces
   FieldDescriptor = new
   {
       name = "height",
       xpath = "@height",
   }}, // here's the closing brace
   new {
   FieldDescriptor3 = new
   {
       name = "width",
       xpath = "@width",
   }},
   objectName = "Job",
   limit = "1",
   xpathFilter = "@openJob='true'"
}

Upvotes: 2

DwayneBull
DwayneBull

Reputation: 264

It looks like in the json "fields" is an array of objects where each object contains a "FieldDescriptor" field. In your C# code you are creating "fields" as an object with multiple fields not an array of objects.

Upvotes: 0

Related Questions