Sun
Sun

Reputation: 6888

JSON not in Correct Format

what's wrong in my JSON:

{
    "id":1,
    "album":"127 Hours",
    "songs":
        [{
            "id":1,
            "name":"Never Hear Surf Music Again",
            "duration":"5:52"
        }],
    "id":2,
    "album":"Adele 21",
    "songs":
        [{
            "id":1,
            "name":"Rolling In The Deep",
            "duration":"03:48"
        }]
}

before using JSON in my app, i am checking it here :

http://json.parser.online.fr/

but getting JSON Eval for id: 2 only, not for both id: 1 & 2

Upvotes: 0

Views: 2888

Answers (5)

Ian Roberts
Ian Roberts

Reputation: 122424

You have a single top-level object with two mappings for each key id, album and songs. This is not valid - the keys of an object should be unique.

You probably need the top-level to be either a list of objects

[
    {
        "id":1,
        "album":"127 Hours",
        "songs": [
            {
                "id":1,
                "name":"Never Hear Surf Music Again",
                "duration":"5:52"
            }
        ]
    },
    {
        "id":2,
        .....

or change the representation slightly and use a top-level object keyed by album ID

{
   "album_1":{
        "title":"127 Hours",
        "songs": [
            {
                "id":1,
                "name":"Never Hear Surf Music Again",
                "duration":"5:52"
            }
        ]
    },
    "album_2":{
        "title":"Adele 21",
         ....

Which representation is more appropriate depends on your use case. The list-of-objects representation lets you iterate over the list of albums in a predictable order but means you need to do a (linear or binary) search to find the album with a particular ID. The top-level object representation lets you find the details for a single album ID more easily but you're no longer guaranteed to get them in the same order when iterating.

Upvotes: 2

Aarun
Aarun

Reputation: 564

{
    "Album list": [
        {
                "id":1,
             "album":"127 Hours",
             "songs":{
    "id":1,
    "name":"Never Hear Surf Music Again",
    "duration":"5:52"
    }
        },
        {
               "id":2,
    "album":"Adele 21",
    "songs":

    {
    "id":1,
    "name":"Rolling In The Deep",
    "duration":"03:48"
    }
        }



    ]
}

Upvotes: 1

Denys Séguret
Denys Séguret

Reputation: 382464

You can't have a property more than once in an object, and you have twice the "id" property, twice "album" and twice "songs".

Even if it's a valid (parsable) JSON string, it doesn't store what you want it to store, or more precisely it can't be restored in an object with all those property values.

You should make it an array of objects :

[
    {

        "id":1,
        "album":"127 Hours",
        "songs":[
          {
          "id":1,
          "name":"Never Hear Surf Music Again",
          "duration":"5:52"
          }
        ]
    }, {
        "id":2,
        "album":"Adele 21",
        "songs":[
          {
          "id":1,
          "name":"Rolling In The Deep",
          "duration":"03:48"
          }
        ]
    }
 ]

Upvotes: 4

Aleks G
Aleks G

Reputation: 57346

I edited your question with proper indentation for your JSON. You can now easily see that you have duplicate properties, specifically, id, album and songs. In a valid JSON you cannot have repeated property names - hence your error.

Most likely, you want to have an array of objects rather than everything together. I suspect, the following JSON will satisfy your needs:

[{
    "id":1,
    "album":"127 Hours",
    "songs":
        [{
            "id":1,
            "name":"Never Hear Surf Music Again",
            "duration":"5:52"
        }]
},
{
    "id":2,
    "album":"Adele 21",
    "songs":
        [{
            "id":1,
            "name":"Rolling In The Deep",
            "duration":"03:48"
        }]
}]

Upvotes: 1

mohkhan
mohkhan

Reputation: 12335

I believe this is how your JSON should be

[{
    "id": 1,
    "album": "127 Hours",
    "songs": [{
        "id": 1,
        "name": "Never Hear Surf Music Again",
        "duration": "5:52"
    }]
}, {
    "id": 2,
    "album": "Adele 21",
    "songs": [{
        "id": 1,
        "name": "Rolling In The Deep",
        "duration": "03:48"
    }]
}]

Upvotes: 1

Related Questions