πŸ”’ JSON Data Types
Estimated reading: 3 minutes 98 views

πŸ”’ JSON Number – Format, Rules, and Examples (2025 Guide)


🧲 Introduction – Understanding Numbers in JSON

In JSON (JavaScript Object Notation), numbers are one of the most commonly used data types. Whether you’re working with prices, counts, percentages, or IDsβ€”numbers are essential for representing numerical values in structured data.

Unlike some programming languages, JSON has strict rules for formatting numbers. It avoids complex features like hexadecimal or octal, and sticks to a clean decimal format that can be easily parsed across platforms.

🎯 In this guide, you’ll learn:

  • What defines a number in JSON
  • Valid formats (integer, float, exponent)
  • How numbers differ from strings
  • Practical examples and usage in JavaScript

βœ… JSON Number – Format and Rules

In JSON, a number:

  • Can be positive or negative
  • Can be an integer or a decimal
  • Can use scientific (exponential) notation
  • Does NOT support: hexadecimal, NaN, Infinity, or leading zeroes

πŸ“Œ Valid JSON Number Formats

TypeExampleDescription
Integer25Whole number
Negative Number-100Begins with minus sign
Decimal99.99Includes decimal point
Exponential6.02e23Scientific notation (E format)

πŸ§ͺ Example – JSON Object with Numbers

{
  "id": 101,
  "price": 59.99,
  "discount": 0.15,
  "quantity": -2,
  "mass": 1.23e4
}

πŸ” Explanation (Line-by-Line):

  • "id": 101 β†’ Integer
  • "price": 59.99 β†’ Floating point number
  • "discount": 0.15 β†’ Decimal number
  • "quantity": -2 β†’ Negative integer
  • "mass": 1.23e4 β†’ Scientific notation for 12300

🚫 Invalid JSON Number Examples

Here are some examples of invalid numbers in JSON that would cause parsing errors:

{
  "hex": 0xFF,        // ❌ Hexadecimal not allowed
  "octal": 075,       // ❌ Octal format not allowed
  "price": NaN,       // ❌ NaN not allowed
  "infinite": Infinity // ❌ Infinity not supported
}

❗ Fix:

Convert these values to standard decimal numbers or null if unknown.


βš™οΈ JSON Number in JavaScript

If you parse JSON in JavaScript using JSON.parse(), the numbers become native Number types.

βœ… Example:

const json = '{ "score": 85.5 }';
const obj = JSON.parse(json);
console.log(obj.score);  // Output: 85.5
console.log(typeof obj.score);  // Output: number

πŸ“Œ Summary – Recap & Next Steps

JSON numbers are clean, strict, and cross-platform compatible. By avoiding language-specific number formats, JSON ensures consistency when transferring data.

πŸ” Key Takeaways:

  • JSON supports integers, floats, and scientific notation
  • Numbers must not be quoted or formatted with leading zeros
  • Special values like NaN, Infinity, or undefined are not allowed
  • JSON numbers convert to native numbers in most programming languages

βš™οΈ Real-world use:

JSON numbers are used in e-commerce apps (prices), APIs (IDs, counts), IoT data (metrics), and financial systems (transaction values).


❓ FAQ – JSON Number


❓ Can JSON numbers have decimal points?
βœ… Yes. JSON supports floating point numbers like 123.45.


❓ Are negative numbers allowed in JSON?
βœ… Yes. You can use -10, -5.5, etc., as valid JSON values.


❓ Is NaN a valid JSON number?
❌ No. JSON does not allow NaN or Infinity.


❓ Do I need to put quotes around numbers?
❌ No. Numbers must be written without quotes. Quoting them makes them strings.


❓ Can I use exponential notation in JSON?
βœ… Yes. JSON allows scientific notation, like 1.23e4 (which is 12300).


Share Now :

Leave a Reply

Your email address will not be published. Required fields are marked *

Share

Json Number

Or Copy Link

CONTENTS
Scroll to Top