Recent chats
ASKGEEZ Q&A GUIDETechnology & devices

How can I check whether a JSON number is an integer or a decimal value?

Quick answer

Python’s standard JSON decoder converts integer-form number tokens such as 7 to int and fractional-form tokens such as 7.0 to float by default. [S1] Check the decoded value with type(value) is int or type(value) is float to enforce an exact type; the exact int check also rejects booleans. Choose which types and fields are valid from your own schema.

LET’S WORK THROUGH IT

The question, the details, the next step.

Question

I loaded a JSON object in Python and need to tell whether a field contains an integer or a decimal. What should I check?

Geez

After json.loads() or json.load(), check the Python value’s type. Python’s standard JSON decoder maps integer-form number tokens such as 7 to int, and real or fractional-form tokens such as 7.0 to float by default. [S1]

For an exact distinction, use type(value) is int and type(value) is float. This checks what type the decoder produced; it does not decide which type your application ought to accept. That rule belongs to the schema you choose.

Follow-up question

Can you show that with a small schema, including a case where a boolean must not pass as an integer?

Geez

Suppose, as an example schema, each record must have an integer count and a fractional-number ratio. The example below requires ratio to decode as a float; that is a schema choice, not a rule imposed by JSON. Python’s bool is an integer subtype, so isinstance(True, int) is true; the exact-type check avoids treating true as an ordinary integer. [S1]

import json

def valid_record(record):
    return (
        isinstance(record, dict)
        and type(record.get("count")) is int
        and type(record.get("ratio")) is float
    )

samples = [
    '{"count": 3, "ratio": 0.5}',
    '{"count": 3.0, "ratio": 0.5}',
    '{"count": true, "ratio": 0.5}',
    '{"count": 3, "ratio": 1}',
]

for text in samples:
    record = json.loads(text)
    print(record, valid_record(record))

The first sample is accepted. The second has a floating-point count, the third has a boolean count, and the fourth has an integer-form ratio; all three are rejected by this particular schema.

Follow-up question

What if a field is missing or someone sends a numeric value as text? How do I make the result clearer?

Geez

A missing field makes record.get(...) return None, which fails these exact-type checks. A string such as "3" also fails: this code validates the decoded type and deliberately does not convert text into a number. If your schema permits conversion, define that separately rather than silently accepting different input forms.

For clearer feedback, check fields one at a time and report the expected type. For example, you could distinguish “missing count” from “count must be an integer” by testing whether the key exists before checking its value. Decide whether additional fields are allowed too; the example schema above only checks the two named fields and does not reject extras.

Follow-up question

Is checking int versus float enough to know whether a value is valid, and what should I test before using this?

Geez

No. The type check answers whether Python decoded a token as an integer or float; it does not establish your field rules, required fields, allowed ranges, or whether extra fields are acceptable. The Python documentation describes the decoder mappings, but it does not define your application’s schema. [S1]

Before relying on the check, write down each field’s expected type and whether it is required. Then try accepted and rejected examples that match those rules. In particular, test an integer token, a fractional token, true, a missing field, and a numeric string. Keep the sample schema explicit so another reader can see why each input passes or fails.

A practical checklist

  • Write down each field’s expected decoded type and whether it is required.

  • Use type(value) is int when booleans must not count as integers.

  • Use type(value) is float only if your schema requires a fractional-form number to decode as a float.

  • Test integer, fractional, boolean, missing, and numeric-string inputs against the intended rules.

  • Decide separately whether extra fields and numeric conversions are allowed.

A few more useful answers

Why not use `isinstance(value, int)` for an integer field?

Because Python’s bool is an integer subtype, isinstance(True, int) is true. Use type(value) is int when a boolean must be rejected. [S1]

Should `7.0` count as an integer because it has no fractional part?

That depends on your schema. The decoder produces a float for a fractional-form token such as 7.0, so an exact int check rejects it. [S1]

Does this example define a general JSON schema?

No. It illustrates checks for two fields only. The required fields, accepted types, and treatment of extra fields must be specified for your own data.

Sources

References used for this guide.

  1. [S1] Python 3.15 json — JSON encoder and decoder
    docs.python.org · Checked

YOUR QUESTION. YOUR DETAILS.

Let’s work through your situation.

Get a tailored answer or help with your next task. Starts at $1, with a quote before you pay.

Ask your own question
← Back to Geez at Work