0

I receive this data when I call my api

[{"DispatchNo":"xxxxx","DispatchDate":"xxxxxxx","Complete":"xxx","CustomerID":"xxxxx","Name":"xxxxxx","Contact":"","Phone":"xxxxx","ShipPhone":"xxxx","PurchaseOrder":"xxxx","OrderLoads":"5","OrderQty":"125","FreightUnitID":"x 

my controoller.js code where it indicates failure is:

var jsonString = result.data.replace(/\\/g, "\\");
                var orderFromApex = JSON.parse(jsonString);
                orderFromApex = orderFromApex.substring(0);
                console.log(orderFromApex);
                //orderFromApex += '"';
                orderFromApex = JSON.parse(orderFromApex);
                console.log(orderFromApex);

I get error SyntaxError: Unexpected token C in JSON at position 898 at JSON.parse () I believe it is failing at orderFromApex = JSON.parse(orderFromApex). I am also console logging orderFromApex which I have posted above. What is going on? is there a problem with my JSON response?

3
  • 1
    The JSON has some unescaped "'s, which makes it invalid. Specifically the portion: "comment3":"door to office "Call derek before Dumping.""
    – CRice
    Jan 11, 2018 at 20:53
  • 1
    copy your json, go here: jsonlint.com and you will see that the problem is that you have an invalid json response at line 38 Jan 11, 2018 at 20:53
  • 1
    orderFromApex.substring(0); wat
    – Kevin B
    Jan 11, 2018 at 20:57

1 Answer 1

6

Your API seems to not escape characters correctly. " for example appears unescaped inside strings closing them before they should be closed.

{"example": "Hello "World"!"} should instead be {"example": "Hello \"World\"!"}.

Trying to parse the first example will throw SyntaxError: Unexpected token W in JSON at position 20. That's because the parser will look at the " before World and think Oh, the string is already over, what's this weird W doing after it?.

2
  • 1
    To be more clear the sentence "Call derek before Dumping." It breaks the logic and the parser is met with the unexpected character C ...\"Call derek before Dumping.\" should be returned instead. Jan 11, 2018 at 20:56
  • I understand Thank you :)
    – Ackman
    Jan 11, 2018 at 22:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.