Skip to content

json

The json package converts between JSON text and Kiwi values. JSON objects become hashmaps, arrays become lists, and null, booleans and numbers map to their Kiwi equivalents.

text = json::stringify({id: 1, tags: ["kiwi", "json"], active: true})
println text # {"id":1,"tags":["kiwi","json"],"active":true}
data = json::parse(text)
println data["tags"][0] # kiwi
println json::stringify({name: "kiwi", size: 3}, true)

Pass true as the second argument to stringify for indented output. Reading and writing files is a matter of combining json with fio:

config = json::parse(fio::read("config.json"))
fio::write("config.json", json::stringify(config, true))

json::pipeline() loads JSON records (an array of objects, or a single object) with field selection and type conversion. It mirrors the csv pipeline.

rows = json::pipeline()
.select(["id", "score", "active"])
.with_schema({"id": "integer", "score": "float", "active": "boolean"})
.from_string('[{"id": "1", "score": "9.5", "active": "true", "note": "x"}]')
.parse()
println rows # [{"id": 1, "score": 9.5, "active": true}]
MethodEffect
.from_file(path), .from_string(text)Add a source.
.select(fields)Keep only these fields in the sources added after it.
.with_schema(types)Convert fields by name in the sources added after it; the types are the same as for CSV.
.as_dataset(name)Name the most recent source.
.parse()Run the pipeline.

A single top-level object is returned as a one-element list. With named datasets, parse() returns a hashmap of lists.

data = json::pipeline()
.from_file("users.json").as_dataset("users")
.from_file("roles.json").as_dataset("roles")
.parse()
json::parse(input: string): any

Parses a JSON string into a Kiwi value.

ParameterTypeDefaultDescription
inputstringThe JSON-formatted string to parse.

Returns any — A Kiwi value (hashmap, list, string, integer, float, boolean, or null).

json::stringify(value: any, pretty: boolean = false): string

Serializes a Kiwi value to a JSON string.

ParameterTypeDefaultDescription
valueanyThe value to serialize.
prettybooleanfalseWhether to pretty-print the output (default false).

Returns string — A JSON-formatted string.

json::pipeline(): JsonPipeline

Creates a new JsonPipeline builder for fluent JSON loading.

Returns JsonPipeline — A new pipeline builder instance.

A fluent builder for loading and selecting fields from one or more JSON sources.

# Single file
users = json::pipeline().from_file("users.json").parse()
# Select specific fields
rows = json::pipeline()
.select(["id", "name"])
.from_file("users.json")
.parse()
# Multiple named datasets
data = json::pipeline()
.from_file("users.json").as_dataset("users")
.from_file("roles.json").as_dataset("roles")
.parse()
JsonPipeline.new()

Initialises an empty pipeline builder.

select(keys: list): JsonPipeline

Specifies which keys to retain from each object in the next source.

ParameterTypeDefaultDescription
keyslistList of key name strings to keep.

Returns JsonPipeline — self, for chaining.

with_schema(schema: hashmap): JsonPipeline

Sets a type-coercion schema for the next source.

ParameterTypeDefaultDescription
schemahashmapMap of field name to type string.

Returns JsonPipeline — self, for chaining.

from_file(path: string): JsonPipeline

Registers a JSON file as a source using the current pending settings.

ParameterTypeDefaultDescription
pathstringPath to the JSON file.

Returns JsonPipeline — self, for chaining.

Throws string — If the file does not exist at parse time.

from_string(input: string): JsonPipeline

Registers a JSON string as a source using the current pending settings.

ParameterTypeDefaultDescription
inputstringA JSON-formatted string.

Returns JsonPipeline — self, for chaining.

as_dataset(name: string): JsonPipeline

Names the most recently registered source as a dataset.

ParameterTypeDefaultDescription
namestringThe dataset key name.

Returns JsonPipeline — self, for chaining.

Throws string — If called before any source is registered.

parse()

Executes the pipeline and returns parsed results.

Returns list|hashmap — Parsed JSON data.

Throws string — On missing file or mixed dataset/non-dataset sources.