Please note, this is a STATIC archive of website developer.mozilla.org from 03 Nov 2016, cach3.com does not collect or store any user information, there is no "phishing" involved.

Revision 1064898 of SyntaxError: JSON.parse: bad parsing

  • Revision slug: Web/JavaScript/Reference/Errors/JSON_bad_parse
  • Revision title: SyntaxError: JSON.parse: bad parsing
  • Revision id: 1064898
  • Created:
  • Creator: fscholz
  • Is current revision? No
  • Comment new page

Revision Content

{{jsSidebar("Errors")}}

Message

SyntaxError: JSON.parse: unexpected character at line x column y of the JSON data.

SyntaxError: JSON.parse: unexpected end of data at line x column y of the JSON data.

SyntaxError: JSON.parse: expected property name or '}' at line x column y of the JSON data

SyntaxError: JSON.parse: expected ',' or '}' after property value in object at line x column y of the JSON data

SyntaxError: JSON.parse: unterminated fractional number at line 1 column 2 of the JSON data.

Error type

{{jsxref("SyntaxError")}}

What went wrong?

{{jsxref("JSON.parse()")}} parses a string as JSON. This string has to be valid JSON and will throw this error if incorrect syntax was encountered.

Examples

JSON.parse() does not allow trailing commas

Both lines will throw a SyntaxError:

JSON.parse('[1, 2, 3, 4, ]');
JSON.parse('{"foo" : 1, }');
// SyntaxError JSON.parse: unexpected character at line 1 column 14 of the JSON data

Omit the trailing commas to parse the JSON correctly:

JSON.parse('[1, 2, 3, 4 ]');
JSON.parse('{"foo" : 1 }');

Property names must be double-quoted strings

You cannot use single-quotes around properties, like 'foo'.

JSON.parse("{'foo' : 1 }");
// SyntaxError: JSON.parse: expected property name or '}' at line 1 column 2 of the JSON data

Instead write "foo":

JSON.parse('{"foo" : 1 }');

Leading zeros and decimal points

You cannot use leading zeros, like 01, and decimal points must be followed by at least one digit.

JSON.parse('{"foo" : 01 }');
// SyntaxError: JSON.parse: expected ',' or '}' after property value in object at line 1 column 2 of the JSON data

JSON.parse('{"foo" : 1. }'); 
// SyntaxError: JSON.parse: unterminated fractional number at line 1 column 2 of the JSON data

Instead write just 1 without a zero and use at least one digit after a decimal point:

JSON.parse('{"foo" : 1 }');
JSON.parse('{"foo" : 1.0 }');

See also

  • {{jsxref("JSON")}}
  • {{jsxref("JSON.parse()")}}
  • {{jsxref("JSON.stringify()")}}

Revision Source

<div>{{jsSidebar("Errors")}}</div>

<h2 id="Message">Message</h2>

<pre class="syntaxbox">
SyntaxError: JSON.parse: unexpected character at line x column y of the JSON data.

SyntaxError: JSON.parse: unexpected end of data at line x column y of the JSON data.

SyntaxError: JSON.parse: expected property name or '}' at line x column y of the JSON data

SyntaxError: JSON.parse: expected ',' or '}' after property value in object at line x column y of the JSON data

SyntaxError: JSON.parse: unterminated fractional number at line 1 column 2 of the JSON data.
</pre>

<h2 id="Error_type">Error type</h2>

<p>{{jsxref("SyntaxError")}}</p>

<h2 id="What_went_wrong">What went wrong?</h2>

<p>{{jsxref("JSON.parse()")}} parses a string as JSON. This string has to be valid JSON and will throw this error if incorrect syntax was encountered.</p>

<h2 id="Examples">Examples</h2>

<h3 id="JSON.parse()_does_not_allow_trailing_commas"><code>JSON.parse()</code> does not allow trailing commas</h3>

<p>Both lines will throw a SyntaxError:</p>

<pre class="example-bad brush: js example-bad">
JSON.parse('[1, 2, 3, 4, ]');
JSON.parse('{"foo" : 1, }');
// SyntaxError JSON.parse: unexpected character at line 1 column 14 of the JSON data
</pre>

<p>Omit the trailing commas to parse the JSON correctly:</p>

<pre class="example-bad brush: js example-good">
JSON.parse('[1, 2, 3, 4 ]');
JSON.parse('{"foo" : 1 }');</pre>

<h3>Property names must be double-quoted strings</h3>

<p>You cannot use single-quotes around properties, like 'foo'.</p>

<pre class="example-bad brush: js example-bad">
JSON.parse("{'foo' : 1 }");
// SyntaxError: JSON.parse: expected property name or '}' at line 1 column 2 of the JSON data</pre>

<p>Instead write "foo":</p>

<pre class="example-bad brush: js example-good">
JSON.parse('{"foo" : 1 }');</pre>

<h3>Leading zeros and decimal points</h3>

<p>You cannot use leading zeros, like 01, and decimal points must be followed by at least one digit.</p>

<pre class="example-bad brush: js example-bad">
JSON.parse('{"foo" : 01 }');
// SyntaxError: JSON.parse: expected ',' or '}' after property value in object at line 1 column 2 of the JSON data

JSON.parse('{"foo" : 1. }'); 
// SyntaxError: JSON.parse: unterminated fractional number at line 1 column 2 of the JSON data
</pre>

<p>Instead write just 1 without a zero and use at least one digit after a decimal point:</p>

<pre class="example-bad brush: js example-good">
JSON.parse('{"foo" : 1 }');
JSON.parse('{"foo" : 1.0 }');
</pre>

<h2 id="See_also">See also</h2>

<ul>
 <li>{{jsxref("JSON")}}</li>
 <li>{{jsxref("JSON.parse()")}}</li>
 <li>{{jsxref("JSON.stringify()")}}</li>
</ul>
Revert to this revision