JSON
Parse and generate JSON data. Supports both single JSON documents and JSONL (JSON Lines) format.
import Encoding.JSON
Parsing
JSON.parse
String -> Result JSONValue Error
Parse a JSON string into a JSONValue. Returns an error for invalid JSON.
json-str = '{"name": "Kit", "version": 1}'
match JSON.parse json-str
| Ok value -> println "Parsed successfully"
| Err e -> println "Parse error: ${e}"
JSON.to-json
a -> JSONValue
Convert any Kit value to a JSONValue. Works with records, lists, strings, numbers, and booleans.
record = {name: "Kit", version: 1}
json-value = JSON.to-json record
Stringification
JSON.stringify
JSONValue -> String
Convert a JSONValue to a compact JSON string (single line, no extra whitespace).
json = JSON.to-json {name: "Kit", version: 1}
str = JSON.stringify json
# {"name":"Kit","version":1}
JSON.pretty
JSONValue -> String
Pretty-print a JSONValue with indentation and line breaks for readability.
json = JSON.to-json {name: "Kit", tags: ["functional", "typed"]}
println (JSON.pretty json)
# {
# "name": "Kit",
# "tags": [
# "functional",
# "typed"
# ]
# }
JSONL Support
JSONL (JSON Lines) is a format where each line is a valid JSON document. Useful for streaming and log processing.
JSON.parse-lines
String -> Result [JSONValue] Error
Parse a JSONL string into a list of JSONValues, one per line.
jsonl = "{\"id\": 1}\n{\"id\": 2}\n{\"id\": 3}"
match JSON.parse-lines jsonl
| Ok items -> println "Parsed ${List.length items} records"
| Err e -> println "Error: ${e}"
JSON.stringify-lines
[JSONValue] -> String
Convert a list of JSONValues to a JSONL string (one JSON document per line).
items = [
JSON.to-json {id: 1, name: "Alice"},
JSON.to-json {id: 2, name: "Bob"}
]
jsonl = JSON.stringify-lines items
# {"id":1,"name":"Alice"}
# {"id":2,"name":"Bob"}