93

I would like to know, whats the right structure for a list of objects in JSON.

We are using JAXB to convert the POJO's to JSON.

Here is the choices, Please direct me what is right.

foos: [
             foo:{..},
             foo:{..}
      ]

or

   foos : [
           {...},
           {...}
          ]

If the first structure is right, what is the JAXB annotation I should use to get the structure right.

1
  • 2
    "using JAXB to convert the POJO's to JSON".... how?
    – skaffman
    Oct 14, 2010 at 10:12

4 Answers 4

116

The second is almost correct:

{
    "foos" : [{
        "prop1":"value1",
        "prop2":"value2"
    }, {
        "prop1":"value3", 
        "prop2":"value4"
    }]
}
3
  • This doesnt validate on jsonlint :( I am tempted to think jsonlint is wrong though :-| Apr 1, 2016 at 18:07
  • 5
    @RamanpreetSingh - If you're validating via jsonlint, then you'll need to also add quotes around foos at the top level. Apr 1, 2016 at 19:55
  • I added quotes and curly braces at the top level and it works. Thanks! Apr 4, 2016 at 12:26
56

The first example from your question,

foos: [
    foo: { ... },
    foo: { ... }
]

is in invalid syntax. You cannot have object properties inside a plain array.

The second example from your question,

foos: [
    { ... },
    { ... }
]

is right although it is not strict JSON. It's a relaxed form of JSON wherein quotes in string keys are omitted.

Following is the correct one when you want to obey strict JSON:

"foos": [
    { ... },
    { ... }
]

This "Mastering JSON" tutorial by Patrick Hunlock, may help to learn about JSON and this site may help to validate JSON.

0
30

As others mentioned, Justin's answer was close, but not quite right. I tested this using Visual Studio's "Paste JSON as C# Classes"

{
    "foos" : [
        {
            "prop1":"value1",
            "prop2":"value2"
        },
        {
            "prop1":"value3", 
            "prop2":"value4"
        }
    ]
}
0
  1. I use jsonformatter page to test the json validator.
  2. try to use jsonwhatever

example:

list_a = func_generator_of_objects()

json_string = jsonwhatever('foos',list_a)

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.