I'm using node and request js to get
a json object from a 3rd party api. When I try to parse
the data i get an error:
SyntaxError: Unexpected token
I then tried to parse the data in my front-end application which resulted in the following error (see the whitespace after token):
SyntaxError: Unexpected token in JSON at position 294512
So i tried to run it through Postman and see what was wrong. The returned JSON seems valid, and I also ran i through a JSON validator without problems.
After investigating the result a bit more i found that one attribute (Document Type) always had a white space inside double qoutes:
{
"F24434": {
"Posting Date": "29-10-12",
"Open": "No",
"On Hold": "",
"Document Type": " ",
"External Document No.": "",
"Due Date": "29-10-12",
"Description": "xxxx",
"Order No.": "",
"Currency Code": "DKK",
"Remaining Amount": "0",
"Original Amount": "0",
"Amount": "0",
"User ID": "xxx",
"Systemdato": "29-10-12",
"Entry No.": "607121",
"Vessel No.": "",
"Port No.": ""
}
}
I'm not sure if that causes the problem or could it be something else?
Unfortunately I don't have direct access to the API, so I cannot alter the way the data i structured.
Solution It turned out that it was the whitespace inside the double quotes. The solution was to remove all whitespaces before parsing:
.replace(/\s/g, '');
I just had the same problem and in my case it turned out that the string had many null chars appended at the end. Solved it with myString.split(String.fromCharCode(0)).join("")
.
A whitespace inside double quotes seems to me like a perfectly valid string. I don't think that was the issue in your case.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With