-3

When I tried to send to my express server JSON like this one:

"{ name: '...', description: '...' }"

This error had been throwed:

SyntaxError: Unexpected token n ...

Trying to parse JSON like this in Chrome:

"{ 'name': '...', 'description': '...' }"

Leads to this error:

SyntaxError: Unexpected token ' ...

Why parsing those JSONs leads to an error? Especially the second JSON looks valid (using ' instead of ").

cooldave
  • 321
  • 1
  • 11

1 Answers1

1

You can't use single quotes in JSON. String values and keys must be enclosed in double quotes.

A value can be a string in double quotes, or a number, or true or false or null, or an object or an array. These structures can be nested.

A string is a sequence of zero or more Unicode characters, wrapped in double quotes, using backslash escapes. A character is represented as a single character string. A string is very much like a C or Java string.

An object is an unordered set of name/value pairs. An object begins with { (left brace) and ends with } (right brace). Each name is followed by : (colon) and the name/value pairs are separated by , (comma).

JSON Object

Quotes from json.org

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
nikc.org
  • 16,462
  • 6
  • 50
  • 83