Sparlarva
Sparlarva

Reputation: 804

JSON syntax error - JSON.parse

I'm getting the error trying to parse this code (apologies for format if incorrect - I am new to this):

 {
     "message": "Please press 1 or 2 to choose the item from your order",
     "totalPrice":  ${order_res.total_price}, //num
     "OrderNum": ${order_res.order_number}, //num
     "orderID": ${order_res.order_id}, //num
     "customerName": ${order_res.customer_name}, //string
     "itemId": [${id}],
     "itemName": [${items}],
     "itemPrice": [${price}]                      
 }

Error message: Uncaught SyntaxError: Unexpected token d in JSON at position 272

I am sending this JSON as a response, now this response was parsing when received on my backend before I added in the last three items but as they are in the same format I'm not sure why its wrong, could somebody help me please with their more experienced eyes?

Thanks!

Upvotes: 0

Views: 264

Answers (1)

karaxuna
karaxuna

Reputation: 26930

Fix this line:

"customerName": "${order_res.customer_name}", //string

In your code, if order_res.customer_name equals to "konichiwa", then you get:

"customerName": konichiwa

But you need:

"customerName": "konichiwa"

Also, if you have array like this:

"itemName": [${['hello', 'world']}]

You get:

"itemName": [hello, world]

Instead of:

"itemName": ["hello", "world"]

Upvotes: 3

Related Questions