Parse JSON returns unexpected identifier error

12.9k views Asked by At

I am trying to parse the following JSON string that is being returned to me but am getting an unexpected identifier error in the console.

"{"title":"MyApp Companion","push_hash":"ff06b5b775e45409f9ab470b64d672d0","t":"mr","alert":"Ryjjgv","n":"Foo Bar","action":"open the app at specific location","pid":"7V8meRCJaj","badge":"Increment"}" 

I am using zepto but the method $.parseJSON() throws the error.

4

There are 4 answers

0
Sarath On BEST ANSWER

remove your first and last " or replace with '

"{
    "title": "MyApp Companion",
    "push_hash": "ff06b5b775e45409f9ab470b64d672d0",
    "t": "mr",
    "alert": "Ryjjgv",
    "n": "Foo Bar",
    "action": "open the app at specific location",
    "pid": "7V8meRCJaj",
    "badge": "Increment"
}"

to

 '{
        "title": "MyApp Companion",
        "push_hash": "ff06b5b775e45409f9ab470b64d672d0",
        "t": "mr",
        "alert": "Ryjjgv",
        "n": "Foo Bar",
        "action": "open the app at specific location",
        "pid": "7V8meRCJaj",
        "badge": "Increment"
    }'

and there is on online TOOL jsonlint.com , to validate your JSON

1
Andrew On

Try this:

$.parseJSON('{"title":"MyApp Companion","push_hash":"ff06b5b775e45409f9ab470b64d672d0","t":"mr","alert":"Ryjjgv","n":"Foo Bar","action":"open the app at specific location","pid":"7V8meRCJaj","badge":"Increment"}')
0
p.s.w.g On

Because you're using " to delimit the string literal, the console is parsing the " inside the string as a string delimiter. Try using ' as to delimit the literal:

'{"title":"MyApp Companion", ... }'

Alternatively, you can escape all the " inside the string with \:

"{\"title\":\"MyApp Companion\", ... }"
0
Kush On

You might have to escape quotes in your string as your string looks like this:

"your string("your string")"

It should be something like:

"your string(\"your string\")" 

or 'your string("your string")'

or: Remove your first and last quotes

Hope this helps.