π’ 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
| Type | Example | Description |
|---|---|---|
| Integer | 25 | Whole number |
| Negative Number | -100 | Begins with minus sign |
| Decimal | 99.99 | Includes decimal point |
| Exponential | 6.02e23 | Scientific 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 for12300
π« 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, orundefinedare 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 :
