'Uncaught SyntaxError: Unexpected token p in JSON at position 15
I have the Json data , which I am trying to parse in the following fiddle
but its throwing error
Uncaught SyntaxError: Unexpected token p in JSON at position 15
I am unable to paste the json here due to the large size kindly follow the fiddle
var varArray = JSON.parse(jsonData);
console.log(varArray);
Fiddle
https://jsfiddle.net/ffeLtaa6/
any suggestion ?
Solution 1:[1]
You have incorrectly wrapped parts of your JSON in quotes
[{"pricing":"{\"price\": ....
^
This shouldn't be here
...
.... \"standingCharge\": \"y\"}}"}]
^
This shouldn't be here
Alternatively, maybe you ARE supposed to have that part wrapped, but then you need to properly escape all backslashes within that part, i.e. \\
instead of just \
.
In that case, when you do JSON.parse(jsonData)
you would get objects (inside the array) that all have a single property, pricing
, with a value that is itself a JSON string.
Solution 2:[2]
It looks like your JSON data has objects enclosed in quotations.
Take the error at position 15; The string up to that point as interpreted by parse() is:
[{"pricing":"{"
At this point your parse function is looking for a comma to continue with or end bracket to finish this object.
Now, if you skip down the string to position 3312, you'll see the character sequence }}"}
. Those last two brackets match up the ones in the very beginning.
The format of your string should be [{"pricing":"{ ... }"}]
if you want a string under "pricing", or [{"pricing":{" ... "}}]
if you want the object representing that string.
Take a look at how the object you are JSON-ifying is being constructed, I get the feeling you may be doubling down somewhere on a stringify()
function for the parent object members.
Solution 3:[3]
I had a the same error "Uncaught SyntaxError: Unexpected token P in JSON at position 0" the code was working but this message was shown on console with status 200
in my case i managed to avoid the error message by changing the return type from String to Integer
like this:
//before
public String method(){
//code here
return "evrything went as expected";
}
//after
public Integer method(){
//code here
return 0;
}
I think this has something to do with the browser that is expecting to get integers, im gonna update this answer if i discover something else
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | |
Solution 2 | |
Solution 3 |