Bob
Bob

Reputation: 8494

Correct JSON structure

I need to return a leaderboard data in pages via JSON, which is the correct structure, is it this

{
 pages: [
         {
          [
           {user: John,
            rating:11},
           {user: Bob,
            rating: 20},
           {user: Andy,
            rating: 30},
            ...
          ]
         },
         {
          [
           {user: Sally,
            rating: 110},
           {user: Peter,
            rating: 115},
           {user: Jim,
            rating: 350},
            ...
          ]
         },
         ...
        ]
}         

Or is this (correct JSON)

{
    "pages": [
        [
            {
                "user": "John",
                "rating": 11 
            },
            {
                "user": "Bob",
                "rating": 20 
            },
            {
                "user": "Andy",
                "rating": 30 
            } 
        ],
        [
            {
                "user": "Sally",
                "rating": 110 
            },
            {
                "user": "Peter",
                "rating": 115 
            },
            {
                "user": "Jim",
                "rating": 350 
            } 
        ] 
    ]
}

UPDATE: Thanks for all the prompt answers, and yes I did construct the JSON by hand which is obviously not a good idea as some of you have pointed out. The 2nd option is the proper JSON and I have updated it with the correct JSON structure for anyone else that might be reading this in the future.

Upvotes: 0

Views: 465

Answers (4)

user479911
user479911

Reputation:

You might want to check out this JSON validator :) http://www.jsonlint.com/

Upvotes: 1

user557219
user557219

Reputation:

Your first example doesn’t make much sense: pages is an array whose elements are objects but there are no key-value pairs. Your second example makes more sense: pages is an array where which element is in turn another array containing a list of objects.

Note that neither of your examples is valid JSON. As explained in the previous paragraph, your first example has objects with no key-value pairs. Furthermore, in both examples the strings aren’t quoted. In JSON, every string must be quoted, be it a key or a value.

Upvotes: 1

Andrew Marshall
Andrew Marshall

Reputation: 96914

The latter is correct, but you need to enclose all your strings with double quotes. You also used a period instead of a comma after the first closing square bracket.

You may wish to use JSONLint to validate your JSON.

Upvotes: 2

mrcrowl
mrcrowl

Reputation: 1479

For starters, the first option is not valid JSON. The array:

{
 pages: [
         {
          [ <== HERE

...would require a name. E.g.

{
 pages: [
         {
           "SomeName": [

Also, assuming John, Bob, Andy etc, are strings, then they should be:

[
           {user: "John",
            rating:11},
           {user: "Bob",
            rating: 20},
           {user: "Andy",
            rating: 30},
            ...
]

Some would also argue that your dictionary member names should be enquoted.

Upvotes: 0

Related Questions