Error parsing JSON with escaped quotes

advertisements

I am getting the following json object when I call the URL from Browser which I expect no data in it.

"{\"data\":[], \"SkipToken\":\"\", \"top\":\"\"}"

However, when I tried to call it in javascript it gives me error Parsing Json message

dspservice.callService(URL, "GET", "", function (data) {
    var dataList = JSON.parse(data);
)};

This code was working before I have no idea why all of a sudden stopped working and throwing me error.


Since there is nothing wrong with the JSON string you gave us, the only other explanation is that the data being passed to your function is something other than what you listed.

To test this hypothesis, run the following code:

dspservice.callService(URL, "GET", "", handler(data));

function handler(data) {
    var goodData = "{\"data\":[], \"SkipToken\":\"\", \"top\":\"\"}";
    alert(goodData);                         // display the correct JSON string
    var goodDataList = JSON.parse(goodData); // parse good string (should work)
    alert(data);                             // display string in question
    var dataList = JSON.parse(data);         // try to parse it (should fail)
}

If the goodData JSON string can be parsed with no issues, and data appears to be incorrectly-formatted, then you have the answer to your question.

Place a breakpoint on the first line of the handler function, where goodData is defined. Then step through the code. From what you told me in your comments, it is still crashing during a JSON parse, but I'm willing to wager that it is failing on the second parse and not the first.