0

I apologize if this might be similar to my previous question. Why does searching for a node which doesn't exist in JSON parse cause every other valid node to fail?

 json.track.wiki.summary causes all to error
 json.track.name  is fine if used without the previous

//JSON data
{
    "track": {
        "id": "434483410",
        "name": "Written in the Stars",
        "mbid": "",
        "url": "http://www.last.fm/music/Tinie+Tempah+(Feat.+Eric+Turner)/_/Written+in+the+Stars",
        "duration": "208000",
        "streamable": {
            "#text": "0",
            "fulltrack": "0"
        },
        "listeners": "108",
        "playcount": "1523",
        "artist": {
            "name": "Tinie Tempah (Feat. Eric Turner)",
            "mbid": "",
            "url": "http://www.last.fm/music/Tinie+Tempah+(Feat.+Eric+Turner)"
        },
        "toptags": "\n "
    }
}

Subsequent access will error for valid nodes. Eliminate the request for missing data and subsequent request will work. there must be a way to test an object before you use it so it won't FUBAR the data.

This data would be OK

//JSON Data
{
"track": {
    "id": "517047006",
    "name": "Skyscraper",
    "mbid": "a92f853d-ad6d-490e-a820-4cff9cc1f224",
    "url": "http://www.last.fm/music/Demi+Lovato/_/Skyscraper",
    "duration": "222000",
    "streamable": {
        "#text": "1",
        "fulltrack": "0"
    },
    "listeners": "114757",
    "playcount": "1424456",
    "artist": {
        "name": "Demi Lovato",
        "mbid": "faf4cefb-036c-4c88-b93a-5b03dd0a0e6b",
        "url": "http://www.last.fm/music/Demi+Lovato"
    },
    "album": {
        "artist": "Demi Lovato",
        "title": "Unbroken",
        "mbid": "9c195a9b-2db4-4b63-9337-6d8152244742",
        "url": "http://www.last.fm/music/Demi+Lovato/Unbroken",
        "image": [{
            "#text": "http://userserve-ak.last.fm/serve/64s/69672054.png",
            "size": "small"
        }, {
            "#text": "http://userserve-ak.last.fm/serve/126/69672054.png",
            "size": "medium"
        }, {
            "#text": "http://userserve-ak.last.fm/serve/174s/69672054.png",
            "size": "large"
        }, {
            "#text": "http://userserve-ak.last.fm/serve/300x300/69672054.png",
            "size": "extralarge"
        }],
        "@attr": {
            "position": "11"
        }
    },
    "toptags": {
        "tag": [{
            "name": "pop",
            "url": "http://www.last.fm/tag/pop"
        }, {
            "name": "ballad",
            "url": "http://www.last.fm/tag/ballad"
        }, {
            "name": "female vocalists",
            "url": "http://www.last.fm/tag/female%20vocalists"
        }, {
            "name": "inspirational",
            "url": "http://www.last.fm/tag/inspirational"
        }, {
            "name": "demi lovato",
            "url": "http://www.last.fm/tag/demi%20lovato"
        }]
    },
    "wiki": {
        "published": "Sat, 20 Aug 2011 18:25:23 +0000",
        "summary": "The Skyscraper Songfacts says: On November 1, 2010, Demi's publicist announced the Disney star had entered a treatment facility for "
        physical and emotional issues,
        " which was subsequently reported to include an eating disorder and self-cutting. Her first single since her stint in treatment finds the singer/actress finding strength in the wake of a fading relationship and it symbolizes her resilience in the face of her personal issues.",
        "content": "The Skyscraper Songfacts says: On November 1, 2010, Demi's publicist announced the Disney star had entered a treatment facility for "
        physical and emotional issues,
        " which was subsequently reported to include an eating disorder and self-cutting. Her first single since her stint in treatment finds the singer/actress finding strength in the wake of a fading relationship and it symbolizes her resilience in the face of her personal issues.\n \nUser-contributed text is available under the Creative Commons By-SA License and may also be available under the GNU FDL."
    }
}

}

3
  • Are you using var data = $.parseJSON( source ); ?. Commented Jan 11, 2013 at 3:47
  • Your second set of data is not valid JSON Commented Jan 11, 2013 at 3:51
  • Yes: var obj = jQuery.parseJSON(strLastFM); I'm stuck with the JSON as it comes from Last.FM and will be different with any query. Commented Jan 11, 2013 at 4:06

2 Answers 2

2

The error is not causing the other code to fail. When the first line errors it stops execution so that the other code is never reached.

You can add something like this:

if(json && json.track && json.track.wiki){
    // do something with json.track.wiki.summary
}
Sign up to request clarification or add additional context in comments.

Comments

0

in case your json fields are not fixed you have to check for their existence in json result. you can try below code for doing the same:

if (typeof(json.track.wiki) != "undefined")
{
//code for what to do with json here
}

OR

make a function like below and pass node element to verify its existence (Preferred)

function isExists(node) {
    if (node== undefined)
      return ""
    else
      return node
}

//and call it like this

var nodevalue=isExists(json.track.wiki)

by doing this you are making sure that particular node exists before you use it. so that later code also execute properly.

you might get some error executing code above . so dont forget to correct!!

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.