json
The json module provides JSON parsing and serialization.
Import
Section titled “Import”import jsonimport { parse, stringify } from jsonParsing JSON
Section titled “Parsing JSON”Parse a JSON string:
@json_string = '{"name": "Alice", "age": 30}'@data = @json#parse(@json_string)parse_file
Section titled “parse_file”Parse JSON from a file:
@data = @json#parse_file("config.json")parse_typed
Section titled “parse_typed”Parse into a typed record:
record User name: String age: Intfin record.
@json_string = '{"name": "Alice", "age": 30}'@user = @json#parse_typed<User>(@json_string)Generating JSON
Section titled “Generating JSON”stringify
Section titled “stringify”Convert value to JSON string:
@data = {"name": "Alice", "age": 30}@json_string = @json#stringify(@data)@// '{"name":"Alice","age":30}'stringify_pretty
Section titled “stringify_pretty”Pretty-print JSON:
@json_string = @json#stringify_pretty(@data, 2)Output:
{ "name": "Alice", "age": 30}to_file
Section titled “to_file”Write JSON to file:
@json#to_file("output.json", @data)to_file_pretty
Section titled “to_file_pretty”Write pretty JSON to file:
@json#to_file_pretty("output.json", @data, 2)JSON Values
Section titled “JSON Values”@json#Null @// null@json#Bool(true) @// true@json#Int(42) @// 42@json#Float(3.14) @// 3.14@json#String("hello") @// "hello"@json#Array([1, 2, 3]) @// [1,2,3]@json#Object({"key": "value"}) @// {"key":"value"}Validation
Section titled “Validation”is_valid
Section titled “is_valid”Check if string is valid JSON:
@json#is_valid('{"key": "value"}') @// true@json#is_valid('invalid') @// falsevalidate
Section titled “validate”Validate JSON against schema:
@schema = '{ "type": "object", "properties": { "name": {"type": "string"}, "age": {"type": "number"} }, "required": ["name", "age"]}'@result = @json#validate(@json_string, @schema)JSON Path
Section titled “JSON Path”get_at
Section titled “get_at”Get value at JSON path:
@data = @json#parse('{"users": [{"name": "Alice"}, {"name": "Bob"}]}')@name = @json#get_at(@data, "users[0].name") @// "Alice"set_at
Section titled “set_at”Set value at JSON path:
@json#set_at(@data, "users[0].name", "Charlie")delete_at
Section titled “delete_at”Delete value at JSON path:
@json#delete_at(@data, "users[1]")JSON Manipulation
Section titled “JSON Manipulation”Merge two JSON objects:
@a = {"name": "Alice"}@b = {"age": 30}@merged = @json#merge(@a, @b)@// {"name": "Alice", "age": 30}Compute difference between JSON values:
@changes = @json#diff(@old, @new)Apply JSON patch:
@patched = @json#patch(@original, @patch)JSON Pointer
Section titled “JSON Pointer”resolve
Section titled “resolve”Resolve JSON Pointer:
@value = @json#resolve(@data, "/users/0/name")Streaming Parser
Section titled “Streaming Parser”Parser
Section titled “Parser”Parse large JSON incrementally:
@parser = @json#Parser::new("huge_file.json")for @event in @parser match @event when StartObject then write("Object started") when Key then write("Key: " + @event#value) when Value then write("Value: " + @event#value) when EndObject then write("Object ended") fin match.fin for.Error Handling
Section titled “Error Handling”@result = @json#parse(@invalid_json)match @result when ok then write("Parsed: " + string(@result#value)) when err then write("Parse error: " + @result#error)fin match.Converting to Records
Section titled “Converting to Records”record User name: String age: Int email: String?fin record.
@json_string = '{"name": "Alice", "age": 30, "email": "alice@example.com"}'@user = @json#from_json<User>(@json_string)
@json_output = @json#to_json(@user)Schema Generation
Section titled “Schema Generation”Generate JSON Schema from record type:
@schema = @json#schema_for<User>()Compact vs Pretty
Section titled “Compact vs Pretty”@compact = @json#compact(@data)@pretty = @json#pretty(@data, 2)