Working with JSON tables in TypeScript
JSON file handling with automatic format detection and high-performance data operations.
Installation
Section titled “Installation”npm install fairspecGetting Started
Section titled “Getting Started”The JSON plugin provides:
loadJsonTable- Load JSON files into tablessaveJsonTable- Save tables to JSON filesJsonPlugin- Plugin for framework integration
For example:
import { loadJsonTable } from "fairspec"
const table = await loadJsonTable({ data: "table.json" })// Standard JSON array of objects formatBasic Usage
Section titled “Basic Usage”Loading JSON Files
Section titled “Loading JSON Files”import { loadJsonTable } from "fairspec"
// Load from local fileconst table = await loadJsonTable({ data: "data.json" })
// Load from remote URLconst table = await loadJsonTable({ data: "https://example.com/data.json"})
// Load multiple files (concatenated)const table = await loadJsonTable({ data: ["file1.json", "file2.json"]})Saving JSON Files
Section titled “Saving JSON Files”import { saveJsonTable } from "fairspec"
// Save with default optionsawait saveJsonTable(table, { path: "output.json" })
// Save with explicit formatawait saveJsonTable(table, { path: "output.json", format: { name: "json" }})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:
// Input: {"users": [{"id": 1, "name": "Alice"}]}const table = await loadJsonTable({ data: "data.json", format: { name: "json", jsonPointer: "users" }})Column Selection
Section titled “Column Selection”Select specific columns using columnNames:
// Only load specific columnsconst table = await loadJsonTable({ data: "data.json", format: { name: "json", columnNames: ["name", "age"] }})Array Format Handling
Section titled “Array Format Handling”Handle CSV-style array data with rowType: "array":
// Input: [["id", "name"], [1, "Alice"], [2, "Bob"]]const table = await loadJsonTable({ data: "data.json", format: { name: "json", rowType: "array" }})Saving with JSON Pointer
Section titled “Saving with JSON Pointer”Wrap data in a nested structure when saving:
// Output: {"users": [{"id": 1, "name": "Alice"}]}await saveJsonTable(table, { path: "output.json", format: { name: "json", jsonPointer: "users" }})