<?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: Zahid Bhatti</title>
    <description>The latest articles on DEV Community by Zahid Bhatti (@devkittools).</description>
    <link>https://dev.to/devkittools</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%2F4246654%2F2aac8405-b8b0-44af-ba72-ca3beea249bf.png</url>
      <title>DEV Community: Zahid Bhatti</title>
      <link>https://dev.to/devkittools</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/devkittools"/>
    <language>en</language>
    <item>
      <title>JSON.parse can silently turn 1e999 into null — check your formatter in 10 seconds</title>
      <dc:creator>Zahid Bhatti</dc:creator>
      <pubDate>Sun, 15 Nov 2026 23:15:04 +0000</pubDate>
      <link>https://dev.to/devkittools/jsonparse-can-silently-turn-1e999-into-null-check-your-formatter-in-10-seconds-1p79</link>
      <guid>https://dev.to/devkittools/jsonparse-can-silently-turn-1e999-into-null-check-your-formatter-in-10-seconds-1p79</guid>
      <description>&lt;p&gt;Here is a 10-second test you can run on any JSON formatter — including the one built into your own projects:&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;"amount"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mf"&gt;1e999&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Paste it in, format it, and look at what comes back. If the formatter's core is the default JavaScript round-trip — &lt;code&gt;JSON.parse&lt;/code&gt; the text, then &lt;code&gt;JSON.stringify&lt;/code&gt; the result to pretty-print it — the output is:&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;"amount"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The amount is gone. Not flagged, not approximated — replaced with &lt;code&gt;null&lt;/code&gt;, in output that looks completely normal. If that JSON was a payment payload, an invoice, or a config value, the corruption survives every downstream check that only asks "is this valid JSON?" It is valid. It's just wrong.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why it happens
&lt;/h2&gt;

&lt;p&gt;First, what the problem is &lt;em&gt;not&lt;/em&gt;: it is not JSON. JSON itself can represent the original number exactly — &lt;code&gt;1e999&lt;/code&gt; is a perfectly valid JSON number token, carried as text. The limitation is JavaScript's number representation, which is what the text gets converted into the moment it is parsed.&lt;/p&gt;

&lt;p&gt;JavaScript numbers are IEEE 754 doubles. &lt;code&gt;JSON.parse&lt;/code&gt; converts every JSON number into a double, and a double tops out around 1.8 × 10³⁰⁸. So:&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="nb"&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="s1"&gt;'{"amount": 1e999}'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;   &lt;span class="c1"&gt;// { amount: Infinity }&lt;/span&gt;
&lt;span class="nb"&gt;JSON&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;stringify&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;amount&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;Infinity&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="c1"&gt;// '{"amount":null}'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;Infinity&lt;/code&gt; is a legal JavaScript number but it is not a legal JSON value, and &lt;code&gt;JSON.stringify&lt;/code&gt;'s defined behaviour for it is to emit &lt;code&gt;null&lt;/code&gt;. Two reasonable, spec-compliant steps compose into silent data loss.&lt;/p&gt;

&lt;p&gt;To be precise about where the ceiling sits: any parse-then-reserialize pipeline whose number type is an IEEE 754 double inherits the same limit, because the value is already lost at the moment of parsing — before formatting even begins. It is a property of the number representation, not of a particular library. Languages whose default integers are arbitrary-precision behave differently: Python's &lt;code&gt;json&lt;/code&gt; module round-trips &lt;code&gt;12345678901234567890&lt;/code&gt; exactly, because its integers never overflow.&lt;/p&gt;

&lt;h2&gt;
  
  
  It's a family of failures, not one
&lt;/h2&gt;

&lt;p&gt;Overflow is the dramatic case, but the same pipeline quietly alters numbers in at least three other ways. All of these are the real outputs of &lt;code&gt;JSON.parse&lt;/code&gt; → &lt;code&gt;JSON.stringify&lt;/code&gt; in Node:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Input&lt;/th&gt;
&lt;th&gt;Output&lt;/th&gt;
&lt;th&gt;What happened&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;{"amount": 1e999}&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;{"amount": null}&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Overflow → Infinity → serialized as &lt;code&gt;null&lt;/code&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;{"amount": 1e-999}&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;{"amount": 0}&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Underflow → flushed to zero&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;{"id": 12345678901234567890}&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;{"id": 12345678901234567000}&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Integer beyond double precision, rounded&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;{"big": 9007199254740993}&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;{"big": 9007199254740992}&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;One past &lt;code&gt;Number.MAX_SAFE_INTEGER&lt;/code&gt; — off by one&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The last two are the ones that should worry you most. An ID of &lt;code&gt;9007199254740993&lt;/code&gt; coming back as &lt;code&gt;9007199254740992&lt;/code&gt; is still a plausible-looking ID. It will match nothing, join to nothing, and produce no error anywhere. Large integer IDs — from databases, from Snowflake-style generators, from APIs that emit 64-bit values — cross the 2⁵³ boundary routinely.&lt;/p&gt;

&lt;h2&gt;
  
  
  How we found it: in our own tool
&lt;/h2&gt;

&lt;p&gt;We found this class of bug the uncomfortable way: an independent audit of our own JSON formatter fed it &lt;code&gt;{"amount": 1e999}&lt;/code&gt; and watched it print &lt;code&gt;null&lt;/code&gt;. Our formatter was doing exactly what the default &lt;code&gt;JSON.parse&lt;/code&gt; → &lt;code&gt;JSON.stringify&lt;/code&gt; pipeline does, which is precisely the problem — "default" and "safe" are not the same thing here.&lt;/p&gt;

&lt;p&gt;The fix we shipped is deliberately boring. Before parsing, the formatter scans the input's number literals (string-aware, so digits inside quoted text are ignored) and checks each one: if it would overflow to Infinity, underflow to zero, or lose integer precision, the tool &lt;strong&gt;refuses&lt;/strong&gt; and names the literal:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;The number "1e999" cannot be represented exactly (it is too large, and would be silently replaced by null). The input was left unchanged — quote the number as a string if it must pass through as text.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Refusing is a design choice, not the only defensible one. Alternatives a tool (or your own code) can use:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Preserve the literal:&lt;/strong&gt; keep numbers as their original text through the pipeline (several parsers offer this; in JavaScript you can do it with a reviver plus a lossless-JSON library).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Parse as BigInt / decimal&lt;/strong&gt; when the domain is money or 64-bit IDs.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Refuse loudly&lt;/strong&gt; (what we chose): appropriate when the tool's job is formatting, not transformation — the user should decide how an unrepresentable number is handled, not the formatter.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;What is &lt;em&gt;not&lt;/em&gt; defensible is the default: silently emitting a different number — or &lt;code&gt;null&lt;/code&gt; — and letting the user copy it away.&lt;/p&gt;

&lt;h2&gt;
  
  
  The takeaway test
&lt;/h2&gt;

&lt;p&gt;Keep these three lines next to your desk and run them against any tool that touches JSON — formatters, validators, converters, and your own services:&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;"amount"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mf"&gt;1e999&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nl"&gt;"id"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;12345678901234567890&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nl"&gt;"big"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;9007199254740993&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If the output differs from the input and nothing told you, you've found the same bug we did.&lt;/p&gt;




&lt;p&gt;&lt;em&gt;Disclosure: I run &lt;a href="https://devkittools.com/json-formatter" rel="noopener noreferrer"&gt;DevKitTools&lt;/a&gt;, a free set of browser-based developer tools. Its JSON formatter implements the "refuse loudly" behaviour described above — you can verify that with the same three test lines. Everything else in this article is reproducible with the Node commands shown.&lt;/em&gt;&lt;/p&gt;

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