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 ()

marcelo.delta
  • 2,730
  • 5
  • 36
  • 71
  • 3
    JSON need double quotes for strings. Single quote the outside, double quote the inside. – Mark Jul 11 '18 at 18:59
  • 2
    Possible duplicate of [Parsing string as JSON with single quotes?](https://stackoverflow.com/questions/36038454/parsing-string-as-json-with-single-quotes) – Guillaume Georges Jul 11 '18 at 19:00

1 Answers1

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

Samuel Hulla
  • 6,617
  • 7
  • 36
  • 70