Recent chats
ASKGEEZ Q&A GUIDETechnology & devices

How can I read JSON arrays of objects and check each record?

Quick answer

In Python, decode the JSON and check that the result is a list before iterating. Python’s JSON decoder maps arrays to lists and objects to dictionaries; check each item is a dictionary before checking its keys. Use enumerate() to include the array position in findings. Choose the required keys for your application: JSON itself does not specify them. Key checks do not validate the values.

LET’S WORK THROUGH IT

The question, the details, the next step.

Question

I have a JSON array and want to check that every entry is an object with certain keys. What should I check first?

Geez

Decode the JSON, then check the outer value before treating it as a list of records. Python’s JSON decoder maps JSON arrays to lists and JSON objects to dictionaries. Check the top-level value with isinstance(data, list), then each item with isinstance(record, dict) before testing its keys. [S1]

The keys you require depend on your application. For example, if each record must contain name and email, make that list explicit in your validation code. Checking key presence does not establish that a value is non-empty, correctly formatted, or otherwise suitable; add separate checks if those are part of your requirements.

Follow-up question

Can you show a small example that gives me the position of each problem?

Geez

Here is a copyable validation example. It assumes data is the value produced by decoding your JSON. The required keys and record values are placeholders.

required_keys = {"name", "email"}

data = [
    {"name": "<name>", "email": "<email>"},
    {"name": "<name>"},
    "<not-an-object>",
    {"name": "<name>", "email": "<email>"},
]

if not isinstance(data, list):
    print("Problem: top-level JSON value is not an array")
else:
    for position, record in enumerate(data):
        if not isinstance(record, dict):
            print(f"Position {position}: expected an object")
            continue

        missing = required_keys - record.keys()
        if missing:
            print(f"Position {position}: missing keys {sorted(missing)}")

For this sample, the findings are Position 1: missing keys ['email'] and Position 2: expected an object. The loop checks the decoded records and reports findings; it does not add keys or otherwise change them. Check the item’s type before checking its keys so that a non-object item is reported as a separate problem. The decoder’s mapping of JSON arrays and objects to Python lists and dictionaries is what makes these type checks relevant. [S1]

Follow-up question

What if the whole JSON value is an object instead of an array?

Geez

The outer type check catches a valid JSON value that is not an array. A single JSON object decodes to a Python dictionary, so the example reports that the top-level value is not an array and does not enter the record loop. [S1]

Keep that check separate from the per-record checks: the latter are only useful after confirming that the outer value is a list.

Follow-up question

How should I adapt this if I also need to check the values, and how can I keep the findings useful?

Geez

Keep structural checks and value checks distinct. First confirm an item is a dictionary; then, for each required key that exists, apply the rules your application actually needs. For instance, if name must be a non-empty string, check its type and contents explicitly. Finding a key does not prove its value is valid.

For useful findings, include the array position, the key involved, and the problem in plain language. Keep the decoded input unchanged while collecting messages. The JSON decoder’s type mapping does not define your required keys or application-specific value rules; those are decisions for your data requirements. [S1]

A practical checklist

  • Decode the JSON before applying the structural checks.

  • Confirm the top-level value is a list before iterating.

  • Check each item is a dictionary before testing its keys.

  • Write down the required keys for this particular record type.

  • Include the array position and the problem in each finding.

  • Add value checks only for rules your application requires.

A few more useful answers

Does a required-key check validate the values too?

No. It identifies absent keys. Add explicit checks for value type, emptiness, or other application-specific rules if needed.

What does the top-level check tell me?

It distinguishes an array, which decodes to a Python list, from another JSON value such as an object, which decodes to a dictionary. [S1]

Will this validation change the decoded records?

No. The example reads the list and its dictionaries to produce findings; it does not add, remove, or replace data.

Sources

References used for this guide.

  1. [S1] json — JSON encoder and decoder — Python 3.15.2 documentation
    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