1

I am trying to parse an array to JSON.parse which have single quotes around the keys and values. But it is throwing following error.

Uncaught SyntaxError: Unexpected token ' in JSON at position 1

The array which I am passing is :

["{'name': 'Jhon'}"]

Somehow, this string is not throwing any error.

['{"name": "Jhon"}']

Any help will be appreciated. Thanks in advance.

Mahipal
  • 344
  • 2
  • 13

1 Answers1

5

JSON object names must be strings per the JSON spec. ECMA-404

The string specification to look at.

A string is a sequence of Unicode code points wrapped with quotation marks (U+0022).

The object specification to look at.

An object structure is represented as a pair of curly bracket tokens surrounding zero or more name/value pairs. A name is a string.

Which would make {'name': 'Jhon'} invalid since it violates both the string 'Jhon' and name 'name' specifications. Using double quotes is valid JSON { "name": "John" }

Philip Rollins
  • 1,271
  • 8
  • 19
  • Thanks @Philip This answers my question. Guess i will need to change the format of data I am receiving. – Mahipal Apr 23 '20 at 07:39