0

Uncaught SyntaxError: Unexpected token ' in JSON

how to convert this string to object in javascript?

JSON.parse( "[{'dia': '1', 'valor': '0,00'}, {'dia': '2', 'valor': '0,00'}, {'dia': '3', 'valor': '0,00'}]" );

VM85380:1 Uncaught SyntaxError: Unexpected token ' in JSON at position 0 at JSON.parse ()

2
  • 3
    JSON need double quotes for strings. Single quote the outside, double quote the inside. Commented Jul 11, 2018 at 18:59
  • 2
    Possible duplicate of Parsing string as JSON with single quotes? Commented Jul 11, 2018 at 19:00

1 Answer 1

2

JSON always needs to preceeded with single to double quotes, if you're parsing a String.

change your line to:

JSON.parse( '[{"dia": 1, "valor": "0,00"}, {"dia": 2, "valor": "0,00"}, {"dia": 3, "valor": "0,00"}]' );

PS: If you want to store your valor as a float, you should change its format from 0,00 to 0.00

Sign up to request clarification or add additional context in comments.

Comments

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.