OpenViSTAI · Agents · Blockchain · Crypto
Menu

OPENVIST LEARN · FREE LESSONS

JSON and checking the shape of data

Decode a JSON document and verify its structure before using fields.

Lesson 16 of 22 · About 25 minutes · Beginner · English

By the end of this lesson

Distinguish valid JSON syntax from a valid record list.

Before you start

  • Python 3.12 or a compatible newer Python 3 interpreter; no external packages.
  • Complete lesson 15 in this route, or be comfortable with its examples.

Three words to know

JSON
A text format for values such as objects, arrays, strings and numbers.
Decode
Turn a JSON document into Python values.
Schema rule
A requirement about the structure or meaning of data.

1. Decode the document

json.loads reads JSON text; it does not execute Python source. JSON objects become dictionaries and arrays become lists. JSON uses double-quoted strings and lowercase true, false and null; Python uses True, False and None. A syntax parser does not decide whether fields make sense for your application.

A small record listPYTHON · save as lesson.py, then run with your interpreter
import json

text = '[{"status": "open"}, {"status": "closed"}]'
records = json.loads(text)
print(type(records).__name__)
print(records[0]["status"])
What you should see · example output
list
open

2. Validate before counting

This function checks the outer list and each required field. A missing field is different from a present but unsupported value. Extra fields are deliberately allowed here. This short example is for already-decoded small data; it is not a bounded file reader.

State the shape rulePYTHON · save as lesson.py, then run with your interpreter
def check_records(records):
    if not isinstance(records, list):
        raise ValueError("expected a list")
    for record in records:
        if not isinstance(record, dict):
            raise ValueError("expected objects")
        if record.get("status") not in ("open", "closed"):
            raise ValueError("unsupported status")
    return len(records)

print(check_records([{"status": "open"}]))
What you should see · example output
1

3. Serialise a result

json.dumps converts supported Python values into JSON text. indent makes it readable; sort_keys gives a predictable key order for review. Python’s default JSON decoder accepts some extensions such as NaN, so strict interoperability may need extra rules. The final project rejects those constants explicitly.

Display JSON outputPYTHON · save as lesson.py, then run with your interpreter
import json

result = {"records": 2, "open": 1}
print(json.dumps(result, sort_keys=True))
What you should see · example output
{"open": 1, "records": 2}

YOUR TURN

A small check before you continue

Is {"status": "open"} valid JSON? Does it meet the record-list rule above?

Show a suggested answer

It is valid JSON syntax but it is an object, not the required outer list. The corresponding valid input is [{"status": "open"}].

Sources and scope

Documentation and examples reviewed 2026-10-18. Provider screens, package versions and output wording can differ. Examples show expected outcomes; they are not a claim that OpenViST connected to your server or verified your setup.