<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: ToolPeko</title>
    <description>The latest articles on DEV Community by ToolPeko (@toolpeko).</description>
    <link>https://dev.to/toolpeko</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F4245875%2F48deca52-c974-4c68-984d-2e8086e7b360.png</url>
      <title>DEV Community: ToolPeko</title>
      <link>https://dev.to/toolpeko</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/toolpeko"/>
    <language>en</language>
    <item>
      <title>Your JSON Is Invalid. Here Is How to Find the Exact Character That Broke It</title>
      <dc:creator>ToolPeko</dc:creator>
      <pubDate>Sun, 22 Nov 2026 11:17:16 +0000</pubDate>
      <link>https://dev.to/toolpeko/your-json-is-invalid-here-is-how-to-find-the-exact-character-that-broke-it-3636</link>
      <guid>https://dev.to/toolpeko/your-json-is-invalid-here-is-how-to-find-the-exact-character-that-broke-it-3636</guid>
      <description>&lt;p&gt;Every developer knows this moment. You paste a JSON response into your editor, or you load a config file, and the only feedback you get is one short line:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Unexpected token t in JSON at position 1234
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Or worse, the newer message that tells you almost nothing:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Unexpected token 't', ..."t": "value"... is not valid JSON
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The data is broken somewhere. The error knows where. Your job is to make it tell you, in a way a human can act on. This post covers the handful of mistakes that cause almost every invalid JSON error, and a small trick to turn that position number into the exact character that failed.&lt;/p&gt;

&lt;h2&gt;
  
  
  First, what "position" actually means
&lt;/h2&gt;

&lt;p&gt;The position in a JSON.parse error is a character count, starting at zero, counted from the very first character of the string. It is not a line number and it is not a column. Position 1234 means the parser read 1234 characters happily, then hit something it could not accept.&lt;/p&gt;

&lt;p&gt;That sounds useless until you print the characters around it. Here is the whole trick, in plain JavaScript:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;showJsonError&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;text&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;match&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;position&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;\&lt;span class="n"&gt;d&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;exec&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;err&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;message&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="n"&gt;match&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nb"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"No position in this error message."&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nb"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;err&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;message&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;pos&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;Number&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;match&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;start&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;max&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;pos&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;40&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;end&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;min&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;text&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;length&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;pos&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;40&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nb"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;text&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;slice&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;start&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;end&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nb"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;" "&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;repeat&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;pos&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;start&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="s2"&gt;"^ right here"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Feed it the raw string and the caught error, and you get the neighbourhood of the failure with a small arrow under the character that stopped the parser. Nine times out of ten, you will see the problem the moment that window prints.&lt;/p&gt;

&lt;h2&gt;
  
  
  The five mistakes that cause almost everything
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;1. The trailing comma.&lt;/strong&gt; JSON does not allow a comma after the last item in an object or array. JavaScript does, which is why this one slips in from hand-written files all the time:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;"name"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"report"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nl"&gt;"pages"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;12&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That final comma is the single most common cause of invalid JSON in config files and fixtures.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2. Single quotes.&lt;/strong&gt; JSON strings must use double quotes. &lt;code&gt;'name': 'report'&lt;/code&gt; is fine JavaScript and invalid JSON. This one usually arrives from Python, where a dict printed with single quotes looks like JSON but is not.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3. Unquoted keys.&lt;/strong&gt; Every key in JSON is a string, so every key needs its double quotes. &lt;code&gt;{name: "report"}&lt;/code&gt; fails at the very first key.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;4. Comments.&lt;/strong&gt; JSON has no comments. A &lt;code&gt;// note to self&lt;/code&gt; inside a .json file breaks the parse at the slash. If you need comments in a config file, you want JSONC or JSON5, and a parser that accepts them on purpose, not by accident.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;5. Values JSON does not have.&lt;/strong&gt; &lt;code&gt;NaN&lt;/code&gt;, &lt;code&gt;Infinity&lt;/code&gt;, &lt;code&gt;undefined&lt;/code&gt; and functions are not JSON values. &lt;code&gt;JSON.stringify&lt;/code&gt; quietly turns some of them into &lt;code&gt;null&lt;/code&gt;, which means the file you saved is valid but no longer says what you meant. That is the worst kind of bug, because nothing ever throws.&lt;/p&gt;

&lt;h2&gt;
  
  
  A quick way to check before you dig
&lt;/h2&gt;

&lt;p&gt;When the input is long, do not read it top to bottom. Work in this order:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Run it through the position trick above and look at the window it prints.&lt;/li&gt;
&lt;li&gt;If the window looks clean, the problem is often just before it. Parsers report where they gave up, and a missing quote or bracket earlier can make a healthy character look guilty.&lt;/li&gt;
&lt;li&gt;Check the end of the string. A truncated response, cut off mid-value by a network hiccup or a file copy that stopped early, fails at the last character and the start of the file will look perfect.&lt;/li&gt;
&lt;li&gt;If the JSON came from another language, look for single quotes and Python or Java values like &lt;code&gt;True&lt;/code&gt;, &lt;code&gt;None&lt;/code&gt; and &lt;code&gt;NaN&lt;/code&gt; wearing JSON clothes.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  When you would rather not write the helper
&lt;/h2&gt;

&lt;p&gt;I keep the little function above in a scratch file, but on a machine that is not mine, or with a response I should not paste into a random tab, I use a browser-based formatter that shows the error position and highlights the spot in the text. Full disclosure: I build ToolPeko, a site of free tools that run in the browser, and its &lt;a href="https://toolpeko.com/json-formatter/" rel="noopener noreferrer"&gt;JSON formatter&lt;/a&gt; is the one I reach for. The formatting happens on your own device, which matters when the JSON holds anything private, like tokens from a real API response.&lt;/p&gt;

&lt;p&gt;That last point is worth keeping even if you never use the tool. A JSON string from production often carries emails, session tokens and internal URLs. Pasting it into the first online formatter you find means sending it to a stranger's server. A formatter that runs in the page, or a local helper like the one above, keeps the data where it belongs.&lt;/p&gt;

&lt;h2&gt;
  
  
  The short version
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;The position number is a character offset. Print the text around it and the bug usually shows itself.&lt;/li&gt;
&lt;li&gt;Trailing commas, single quotes, unquoted keys, comments and non-JSON values cause almost every failure.&lt;/li&gt;
&lt;li&gt;A parser points at where it gave up, which can be a few characters after the real mistake.&lt;/li&gt;
&lt;li&gt;Keep private JSON off other people's servers. Format it locally or in the browser.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Once you have seen the arrow trick work on one ugly production response, you will never squint at a raw position number again.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>json</category>
      <category>beginners</category>
    </item>
  </channel>
</rss>
