Join Nostr
2026-08-14 23:14:01 UTC
in reply to

solo on Nostr: want to parse json in javascript and it has numbers larger than ...

want to parse json in javascript and it has numbers larger than Number.MAX_SAFE_INTEGER?
well, you just simply,<code>const isBigNumber = num =&gt; !Number.isSafeInteger(+num)<br><br>const enquoteBigNumber = (jsonString, bigNumChecker) =&gt;<br> jsonString<br> .replaceAll(<br> /([:\s\[,]*)(\d+)([\s,\]]*)/g,<br> (matchingSubstr, prefix, bigNum, suffix) =&gt;<br> bigNumChecker(bigNum)<br> ? `${prefix}"${bigNum}"${suffix}`<br> : matchingSubstr<br> )<br><br>const parseWithBigInt = (jsonString, bigNumChecker) =&gt;<br> JSON.parse(<br> enquoteBigNumber(jsonString, bigNumChecker),<br> (key, value) =&gt;<br> !isNaN(value) &amp;&amp; bigNumChecker(value)<br> ? BigInt(value)<br> : value<br> )<br><br>const output = parseWithBigInt(input, isBigNumber)<br></code>