JSON parsing and querying. import json.
Parse a JSON string into a Value.
let v = json.parse("{\"name\": \"Alice\"}")
Convert a Value to a JSON string.
json.stringify(person.encode())
Get a nested value by key. Returns none if key doesn't exist.
json.get(j, "name")
Get all keys of a JSON object as a list of strings.
json.keys(j)
Create a Json string value.
json.from_string("hello")
Create a Json integer value.
json.from_int(42)
Create a Json boolean value.
json.from_bool(true)
Create a Json null value.
json.null()
Create a Json array from a list of Json values.
json.array([json.i(1), json.i(2)])
Create a Json float value.
json.from_float(3.14)
Convert a Json value to a pretty-printed JSON string with indentation.
json.stringify_pretty(j)
Create a Json object from a list of (key, value) pairs.
json.object([("name", json.s("Alice")), ("age", json.i(30))])
Get a string value by key. Returns none if key doesn't exist or value is not a string.
json.get_string(j, "name")
Get an integer value by key. Returns none if key doesn't exist or value is not an integer.
json.get_int(j, "age")
Get a float value by key. Returns none if key doesn't exist or value is not a number.
json.get_float(j, "price")
Get a boolean value by key. Returns none if key doesn't exist or value is not a boolean.
json.get_bool(j, "active")
Get an array value by key. Returns none if key doesn't exist or value is not an array.
json.get_array(j, "items")
Extract string from a Json value (without key lookup). Returns none if not a string.
json.as_string(j)
Extract integer from a Json value (without key lookup). Returns none if not an integer.
json.as_int(j)
Extract float from a Json value (without key lookup). Returns none if not a number.
json.as_float(j)
Extract boolean from a Json value (without key lookup). Returns none if not a boolean.
json.as_bool(j)
Extract array from a Json value (without key lookup). Returns none if not an array.
json.as_array(j)
Create a root JSON path for traversal.
json.root()
Extend a JSON path with a field name.
json.field(json.root(), "user")
Extend a JSON path with an array index.
json.index(json.field(json.root(), "items"), 0)
Get a value at a JSON path. Returns none if path doesn't exist.
json.get_path(j, json.field(json.root(), "name"))
Set a value at a JSON path. Returns error if path is invalid.
json.set_path(j, json.field(json.root(), "name"), json.s("Bob"))
Remove a value at a JSON path. Returns the Json with the value removed.
json.remove_path(j, json.field(json.root(), "temp"))
Convert a JSON object to a Map[String, String]. Values are stringified. Returns none if not an object.
let m = json.to_map(obj) ?? map.new()