Working with JSON tables in Python
JSON file handling with automatic format detection and high-performance data operations.
Installation
Section titled “Installation”pip install fairspecGetting Started
Section titled “Getting Started”The JSON plugin provides:
load_json_table- Load JSON files into tablessave_json_table- Save tables to JSON filesJsonPlugin- Plugin for framework integration
For example:
from fairspec import load_json_table, Resource
table = load_json_table(Resource(data="table.json"))# Standard JSON array of objects formatBasic Usage
Section titled “Basic Usage”Loading JSON Files
Section titled “Loading JSON Files”from fairspec import load_json_table, Resource
# Load from local filetable = load_json_table(Resource(data="data.json"))
# Load from remote URLtable = load_json_table(Resource(data="https://example.com/data.json"))
# Load multiple files (concatenated)table = load_json_table(Resource(data=["file1.json", "file2.json"]))Saving JSON Files
Section titled “Saving JSON Files”from fairspec import save_json_tablefrom fairspec_metadata import JsonFileDialect
# Save with default optionssave_json_table(table, path="output.json")
# Save with explicit formatsave_json_table(table, path="output.json", fileDialect=JsonFileDialect())Standard Format
Section titled “Standard Format”JSON tables use an array of objects format:
[ {"id": 1, "name": "Alice", "age": 30}, {"id": 2, "name": "Bob", "age": 25}]Advanced Features
Section titled “Advanced Features”JSON Pointer Extraction
Section titled “JSON Pointer Extraction”Extract data from nested objects using jsonPointer:
from fairspec import load_json_table, Resourcefrom fairspec_metadata import JsonFileDialect
# Input: {"users": [{"id": 1, "name": "Alice"}]}table = load_json_table(Resource( data="data.json", fileDialect=JsonFileDialect(jsonPointer="users"),))Column Selection
Section titled “Column Selection”Select specific columns using columnNames:
from fairspec import load_json_table, Resourcefrom fairspec_metadata import JsonFileDialect
# Only load specific columnstable = load_json_table(Resource( data="data.json", fileDialect=JsonFileDialect(columnNames=["name", "age"]),))Array Format Handling
Section titled “Array Format Handling”Handle CSV-style array data with rowType: "array":
from fairspec import load_json_table, Resourcefrom fairspec_metadata import JsonFileDialect
# Input: [["id", "name"], [1, "Alice"], [2, "Bob"]]table = load_json_table(Resource( data="data.json", fileDialect=JsonFileDialect(rowType="array"),))Saving with JSON Pointer
Section titled “Saving with JSON Pointer”Wrap data in a nested structure when saving:
from fairspec import save_json_tablefrom fairspec_metadata import JsonFileDialect
# Output: {"users": [{"id": 1, "name": "Alice"}]}save_json_table(table, path="output.json", fileDialect=JsonFileDialect( jsonPointer="users",))