Utilizing node and request js, I am attempting to retrieve a json object from a 3rd party api. However, when trying to decode the data, an error is thrown:
SyntaxError: Unexpected token
My next attempt was to parse the data in my front-end application which resulted in the following error (notice the whitespace after token):
SyntaxError: Unexpected token in JSON at position 294512
To troubleshoot further, I ran it through Postman to identify any issues. Surprisingly, the returned JSON appeared to be valid, and validation through a JSON validator did not raise any errors.
Upon closer inspection of the result, it became evident that one attribute (Document Type) consistently contained a white space within double quotes:
{
"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.": ""
}
}
Could this isolated issue with the white space be causing the problem or is there another underlying issue?
Regrettably, as I do not have direct access to the API, modifying the data structure directly is not an option for me.
Solution: It appears that removing all whitespaces before parsing resolves the issue:
.replace(/\s/g, '');