Skip to content
Tutorials Center
My learning

Topics

Lesson 6 of 39 ยท JSON in Depth

JSON Types, Numbers and Dates

See how JSON maps to language types, why floating-point and 64-bit integers cause bugs, how to store money, dates and binary data, and how to preserve precision.

JSON has only six types, so real-world values such as dates, money, binary files and huge IDs must be squeezed into them. The way you squeeze determines whether data survives a trip between systems. This lesson covers the cases that cause the most bugs.

Mapping to language types

JSON JavaScript Python Java
object Object dict Map / class
array Array list List
string string str String
number number int or float int, long, double, BigDecimal
true / false boolean bool boolean
null null None null

JSON itself says nothing about integer versus floating point. 1, 1.0 and 1e0 are all just numbers; each language decides how to read them.

Numbers are doubles in JavaScript

JavaScript reads every JSON number as a 64-bit float (an IEEE 754 double). That gives about 15-17 significant digits and exact integers only up to 2^53 - 1 (9,007,199,254,740,991).

Node.js Floating-point surprises

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

9007199254740993 silently became 9007199254740992. Databases with 64-bit IDs (Twitter-style snowflake IDs, for instance) hit this constantly. The usual fix is to send large identifiers as strings.

Reading big integers safely

The JSON.parse reviver can receive the original source text in modern engines (context.source), letting you build a BigInt. This is available in Node 24.

Node.js Keeping a big ID exact with BigInt

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Money

Never store currency as a binary float: 0.1 + 0.2 is not 0.3. Two safe options:

  • Integer minor units: {"amount": 1999, "currency": "USD"} means 19.99 dollars.
  • Decimal string: {"amount": "19.99", "currency": "USD"}, parsed by a decimal library.
Node.js Money as integer cents

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Dates and times

JSON has no date type. The universal convention is an ISO 8601 string in UTC, such as "2026-03-14T09:30:00Z". It sorts correctly as text and every language can parse it. Avoid ambiguous strings such as "03/04/2026" and avoid local times without an offset.

Node.js Dates in JSON

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Unix timestamps (1773480600) are also common; say in your documentation whether they are seconds or milliseconds.

Binary data

Raw bytes cannot appear in a JSON string. Encode them as Base64 text, which grows the data by about a third, or send the file separately and put a URL in the JSON.

Node.js Base64 for binary data

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

null versus missing

{"nickname": null} says "the field exists and has no value", while omitting nickname says nothing about it. APIs should document which they use; the difference matters for partial updates, where omitted means "leave alone" and null means "clear it".

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Why can a 64-bit ID such as 9007199254740993 break in JavaScript?

    Doubles cannot represent every integer above Number.MAX_SAFE_INTEGER.
  2. 2. Which is the safest way to represent 19.99 US dollars?

    Integer minor units avoid binary floating-point rounding.
  3. 3. What is the standard way to write a date in JSON?

    JSON has no date type; ISO 8601 strings are unambiguous and sortable.
  4. 4. How should binary data be placed in JSON?

    Base64 turns bytes into text safe for a JSON string.

Summary

  • JSON has six types; everything else needs a convention.
  • JavaScript numbers are doubles, so send huge IDs as strings or parse them as BigInt.
  • Store money as integer minor units or decimal strings.
  • Use ISO 8601 UTC strings for dates and Base64 for bytes.
  • Decide and document the difference between null and a missing field.

Next: JSON.parse and JSON.stringify