bhylu.com

Advertisements

Pandas Read JSON

JSON can represent records, columns or another structured layout. read_json needs the matching orientation rather than a guess based on a familiar file extension.

How it works

Use StringIO for literal JSON text so the API receives a text source rather than an ambiguous path-like string. Explicit dtypes and a required-field check make the imported contract clear. JSON parsing does not ensure unique identifiers, valid ranges or complete records; those remain application checks. Large or nested documents may need normalization and bounded ingestion rather than one unrestricted in-memory read.

Run the code

Select Try in playground to load the complete editable source. Nothing executes until you choose Run. The browser uses Pyodide 0.27.7, Pandas 2.2.3 and NumPy 2.0.2. Each ordinary execution starts a fresh isolated runtime. The tables are fictional and do not use personal files.

For local work, use a separate virtual environment and run python read-json.py. Download the complete source. The reviewed local environment uses Python 3.12.14, Pandas 3.0.6 and NumPy 2.5.4; the browser uses Pyodide 0.27.7 with Pandas 2.2.3 and NumPy 2.0.2. Compatible examples are checked in both. See the environment setup. No Apache or database server is required.

Example

read-json.py

import pandas as pd
import numpy as np

from io import StringIO
source = StringIO('[{"drop_id":101,"kg":12},{"drop_id":102,"kg":8}]')
table = pd.read_json(source, orient="records", dtype={"drop_id":"int64", "kg":"float64"})
print(table["drop_id"].tolist())
print(float(table["kg"].sum()))
print(table["kg"].dtype.name)

Expected result

[101, 102]
20.0
float64

The two records become two rows with an explicit numeric weight type. A different JSON orientation would need a different parser contract even if it contained the same visible numbers. Keep source shape and table grain aligned.

Use Pandas’s official reference for precise contracts, supported versions and numerical limitations.