0

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!

3
  • 2
    Comments are not allowed in JSON. Also, all keys must be Strings, and allowed values are only String, Number, Boolean which may be comprised in the form of either Object, or Array. Commented Aug 22, 2018 at 6:49
  • did your json contains circular dependency? if no, then please provide error message Commented Aug 22, 2018 at 6:51
  • @connexo True, I put them in for context for stackoverflow viewers but i should have specified :) & thanks for the extra info Commented Aug 22, 2018 at 6:54

1 Answer 1

3

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"]
Sign up to request clarification or add additional context in comments.

3 Comments

I previously changed that but had another error but you have opened up what the next error is, that in the itemName it is an array which contains what is supposed to be strings but is in this format [sparlarva sparlarva, sparlarva sparlarva] when it should be ['sparlarva sparlarva', 'sparlarva sparlarva'] right?
@Sparlarva is it es6 template string?
customerName isnt an array until i add the brackets around it in this. it is currently - "hello, world" so i've added the brackets around it so basically it is currently ["hello, world"] - so it's not actually in the correct format because im basically taking the data from another json response and concatenating the data into it and havent yet turned them into seperate strings

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.