<?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: head_of_david</title>
    <description>The latest articles on DEV Community by head_of_david (@head_of_david).</description>
    <link>https://dev.to/head_of_david</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%2F4188664%2Fc0de1076-c987-4141-b747-63d4cf36c0c8.png</url>
      <title>DEV Community: head_of_david</title>
      <link>https://dev.to/head_of_david</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/head_of_david"/>
    <language>en</language>
    <item>
      <title>Unexpected Token in JSON at Position 0: What It Means and How to Fix It</title>
      <dc:creator>head_of_david</dc:creator>
      <pubDate>Fri, 16 Oct 2026 11:07:32 +0000</pubDate>
      <link>https://dev.to/head_of_david/unexpected-token-in-json-at-position-0-what-it-means-and-how-to-fix-it-2efo</link>
      <guid>https://dev.to/head_of_david/unexpected-token-in-json-at-position-0-what-it-means-and-how-to-fix-it-2efo</guid>
      <description>&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SyntaxError: Unexpected token '&amp;lt;', "&amp;lt;!DOCTYPE "... is not valid JSON
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Older browsers and Node versions word it as &lt;code&gt;Unexpected token &amp;lt; in JSON at position 0&lt;/code&gt;. Either way, the message is telling you something specific: the first character of what you tried to parse as JSON was &lt;code&gt;&amp;lt;&lt;/code&gt; — and JSON never starts with &lt;code&gt;&amp;lt;&lt;/code&gt;. &lt;strong&gt;You received HTML, not JSON.&lt;/strong&gt; The parser is fine; the response isn't what you think it is.&lt;/p&gt;

&lt;h2&gt;
  
  
  The messages, decoded
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Message&lt;/th&gt;
&lt;th&gt;What you actually received&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;Unexpected token '&amp;lt;', "&amp;lt;!DOCTYPE "...&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;An HTML page — usually an error page, a login page or your app's &lt;code&gt;index.html&lt;/code&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;Unexpected end of JSON input&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;An empty body&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;"[object Object]" is not valid JSON&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;An object you already parsed, passed to &lt;code&gt;JSON.parse&lt;/code&gt; again&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;"undefined" is not valid JSON&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;The string &lt;code&gt;"undefined"&lt;/code&gt; — often from &lt;code&gt;localStorage&lt;/code&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;code&gt;Unexpected token '﻿'&lt;/code&gt; (an invisible character)&lt;/td&gt;
&lt;td&gt;A UTF-8 byte order mark before the JSON&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;Expected double-quoted property name&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;A trailing comma, or single-quoted keys&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Firefox says &lt;code&gt;JSON.parse: unexpected character at line 1 column 1 of the JSON data&lt;/code&gt;, and Safari says &lt;code&gt;JSON Parse error: Unrecognized token '&amp;lt;'&lt;/code&gt;. Python's equivalent is &lt;code&gt;JSONDecodeError: Expecting value: line 1 column 1 (char 0)&lt;/code&gt;, which &lt;code&gt;requests&lt;/code&gt; raises from &lt;code&gt;.json()&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 1: look at what you got
&lt;/h2&gt;

&lt;p&gt;Read the body as text before parsing it:&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;const&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;fetch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;/api/items&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;text&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;text&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="nx"&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="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;status&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;headers&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;content-type&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="nx"&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="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;200&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Or open DevTools → Network, click the request and look at the &lt;strong&gt;Response&lt;/strong&gt; tab. Nine times out of ten, the answer is right there.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 2: match it to a cause
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;An HTML error page.&lt;/strong&gt; The request hit a &lt;a href="https://www.devkult.com/http/404" rel="noopener noreferrer"&gt;404&lt;/a&gt; or &lt;a href="https://www.devkult.com/http/500" rel="noopener noreferrer"&gt;500&lt;/a&gt;, and the server returned its HTML error page. Check the status code — &lt;code&gt;response.json()&lt;/code&gt; doesn't care whether the request succeeded.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Your app's own &lt;code&gt;index.html&lt;/code&gt;.&lt;/strong&gt; Single-page apps return &lt;code&gt;index.html&lt;/code&gt; for every unknown path, so a wrong API URL (&lt;code&gt;/api/item&lt;/code&gt; instead of &lt;code&gt;/api/items&lt;/code&gt;, or a missing &lt;code&gt;/api&lt;/code&gt; prefix in production) gets HTML with status 200. Also check your dev server proxy: in development &lt;code&gt;/api&lt;/code&gt; may be forwarded to the backend, but in production it isn't.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;A login page.&lt;/strong&gt; An expired session redirects to &lt;code&gt;/login&lt;/code&gt;; &lt;code&gt;fetch&lt;/code&gt; follows the redirect and hands you the login HTML. &lt;code&gt;res.redirected&lt;/code&gt; will be &lt;code&gt;true&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;An empty body.&lt;/strong&gt; &lt;a href="https://www.devkult.com/http/204" rel="noopener noreferrer"&gt;204 No Content&lt;/a&gt; responses and some error responses have no body, so there's nothing to parse — "Unexpected end of JSON input". Check the status before calling &lt;code&gt;.json()&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Parsing twice.&lt;/strong&gt; &lt;code&gt;response.json()&lt;/code&gt; and axios already return objects. Calling &lt;code&gt;JSON.parse()&lt;/code&gt; on the result turns it into the string &lt;code&gt;"[object Object]"&lt;/code&gt; first, which fails.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Not quite JSON.&lt;/strong&gt; Hand-written JSON with trailing commas, comments, single quotes or &lt;code&gt;NaN&lt;/code&gt; isn't valid. Paste it into the &lt;a href="https://www.devkult.com/tools/json/json-validator" rel="noopener noreferrer"&gt;JSON validator&lt;/a&gt; to find the exact position.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 3: guard your fetch calls
&lt;/h2&gt;

&lt;p&gt;Check the status and the &lt;a href="https://www.devkult.com/headers/content-type" rel="noopener noreferrer"&gt;Content-Type&lt;/a&gt; before parsing, and include the start of the body in the error so the next failure explains itself:&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="k"&gt;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;getJson&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;url&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;options&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="nx"&gt;res&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;fetch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;url&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;options&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;type&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;headers&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;content-type&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;??&lt;/span&gt; &lt;span class="dl"&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="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;ok&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nx"&gt;type&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;includes&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;application/json&lt;/span&gt;&lt;span class="dl"&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="nx"&gt;body&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;text&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Error&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="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;status&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt; from &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;url&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="nx"&gt;type&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;no content type&lt;/span&gt;&lt;span class="dl"&gt;"&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="nx"&gt;body&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="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;120&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="p"&gt;}&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;json&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;On the server side, make your API return JSON errors with &lt;code&gt;Content-Type: application/json&lt;/code&gt; — including for 404s and 500s — so clients get a parseable message instead of an HTML page. The correct type for JSON is &lt;a href="https://www.devkult.com/mime/json" rel="noopener noreferrer"&gt;&lt;code&gt;application/json&lt;/code&gt;&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  The localStorage variant
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;JSON&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;parse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;localStorage&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getItem&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;settings&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt; &lt;span class="c1"&gt;// fine when the key exists&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When the key doesn't exist, &lt;code&gt;getItem&lt;/code&gt; returns &lt;code&gt;null&lt;/code&gt;, and &lt;code&gt;JSON.parse(null)&lt;/code&gt; happens to return &lt;code&gt;null&lt;/code&gt; — no error. But if some code once saved &lt;code&gt;localStorage.setItem("settings", undefined)&lt;/code&gt;, the stored value is the string &lt;code&gt;"undefined"&lt;/code&gt;, which throws. Guard and reset:&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;readSettings&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;JSON&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;parse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;localStorage&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getItem&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;settings&lt;/span&gt;&lt;span class="dl"&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="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;catch&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;localStorage&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;removeItem&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;settings&lt;/span&gt;&lt;span class="dl"&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="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To inspect or tidy a JSON payload by hand, use the &lt;a href="https://www.devkult.com/tools/json/json-formatter" rel="noopener noreferrer"&gt;JSON formatter&lt;/a&gt;.&lt;/p&gt;




&lt;p&gt;&lt;em&gt;Originally published at &lt;a href="https://www.devkult.com/blog/unexpected-token-in-json" rel="noopener noreferrer"&gt;devkult.com&lt;/a&gt;. When you need to find exactly where a payload breaks, paste it into the free &lt;a href="https://www.devkult.com/tools/json/json-validator" rel="noopener noreferrer"&gt;JSON validator&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;

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