Mahipal
Mahipal

Reputation: 519

Not able to parse Array of string with single quotes around key value names

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.

Upvotes: 1

Views: 367

Answers (1)

Philip Rollins
Philip Rollins

Reputation: 1291

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" }

Upvotes: 5

Related Questions