json
import jsonFunctions
Section titled “Functions”| Function | Signature | Description |
|---|---|---|
parse | (String) -> Result[Value, String] | Parse a JSON string into a Value. |
stringify | (Value) -> String | Convert a Value to a JSON string. |
get | (Value, String) -> Option[Value] | Get a nested value by key. Returns none if key doesn’t exist. |
keys | (Value) -> List[String] | Get all keys of a JSON object as a list of strings. |
from_string | (String) -> Value | Create a Json string value. |
from_int | (Int) -> Value | Create a Json integer value. |
from_bool | (Bool) -> Value | Create a Json boolean value. |
null | () -> Value | Create a Json null value. |
array | (List[Value]) -> Value | Create a Json array from a list of Json values. |
from_float | (Float) -> Value | Create a Json float value. |
stringify_pretty | (Value) -> String | Convert a Json value to a pretty-printed JSON string with indentation. |
object | (List[(String, Value)]) -> Value | Create a Json object from a list of (key, value) pairs. |
get_string | (Value, String) -> Option[String] | Get a string value by key. Returns none if key doesn’t exist or value is not a string. |
get_int | (Value, String) -> Option[Int] | Get an integer value by key. Returns none if key doesn’t exist or value is not an integer. |
get_float | (Value, String) -> Option[Float] | Get a float value by key. Returns none if key doesn’t exist or value is not a number. |
get_bool | (Value, String) -> Option[Bool] | Get a boolean value by key. Returns none if key doesn’t exist or value is not a boolean. |
get_array | (Value, String) -> Option[List[Value]] | Get an array value by key. Returns none if key doesn’t exist or value is not an array. |
as_string | (Value) -> Option[String] | Extract string from a Json value (without key lookup). Returns none if not a string. |
as_int | (Value) -> Option[Int] | Extract integer from a Json value (without key lookup). Returns none if not an integer. |
as_float | (Value) -> Option[Float] | Extract float from a Json value (without key lookup). Returns none if not a number. |
as_bool | (Value) -> Option[Bool] | Extract boolean from a Json value (without key lookup). Returns none if not a boolean. |
as_array | (Value) -> Option[List[Value]] | Extract array from a Json value (without key lookup). Returns none if not an array. |
root | () -> JsonPath | Create a root JSON path for traversal. |
field | (JsonPath, String) -> JsonPath | Extend a JSON path with a field name. |
index | (JsonPath, Int) -> JsonPath | Extend a JSON path with an array index. |
get_path | (Value, JsonPath) -> Option[Value] | Get a value at a JSON path. Returns none if path doesn’t exist. |
set_path | (Value, JsonPath, Value) -> Result[Value, String] | Set a value at a JSON path. Returns error if path is invalid. |
remove_path | (Value, JsonPath) -> Value | Remove a value at a JSON path. Returns the Json with the value removed. |
Reference
Section titled “Reference”json.parse(text: String) -> Result[Value, String]
Section titled “json.parse(text: String) -> Result[Value, String]”Parse a JSON string into a Value.
let v = json.parse("{\"name\": \"Alice\"}")json.stringify(v: Value) -> String
Section titled “json.stringify(v: Value) -> String”Convert a Value to a JSON string.
json.stringify(person.encode())json.get(j: Value, key: String) -> Option[Value]
Section titled “json.get(j: Value, key: String) -> Option[Value]”Get a nested value by key. Returns none if key doesn’t exist.
json.get(j, "name")json.keys(j: Value) -> List[String]
Section titled “json.keys(j: Value) -> List[String]”Get all keys of a JSON object as a list of strings.
json.keys(j)json.from_string(s: String) -> Value
Section titled “json.from_string(s: String) -> Value”Create a Json string value.
json.from_string("hello")json.from_int(n: Int) -> Value
Section titled “json.from_int(n: Int) -> Value”Create a Json integer value.
json.from_int(42)json.from_bool(b: Bool) -> Value
Section titled “json.from_bool(b: Bool) -> Value”Create a Json boolean value.
json.from_bool(true)json.null() -> Value
Section titled “json.null() -> Value”Create a Json null value.
json.null()json.array(items: List[Value]) -> Value
Section titled “json.array(items: List[Value]) -> Value”Create a Json array from a list of Json values.
json.array([json.i(1), json.i(2)])json.from_float(n: Float) -> Value
Section titled “json.from_float(n: Float) -> Value”Create a Json float value.
json.from_float(3.14)json.stringify_pretty(j: Value) -> String
Section titled “json.stringify_pretty(j: Value) -> String”Convert a Json value to a pretty-printed JSON string with indentation.
json.stringify_pretty(j)json.object(entries: List[(String, Value)]) -> Value
Section titled “json.object(entries: List[(String, Value)]) -> Value”Create a Json object from a list of (key, value) pairs.
json.object([("name", json.s("Alice")), ("age", json.i(30))])json.get_string(j: Value, key: String) -> Option[String]
Section titled “json.get_string(j: Value, key: String) -> Option[String]”Get a string value by key. Returns none if key doesn’t exist or value is not a string.
json.get_string(j, "name")json.get_int(j: Value, key: String) -> Option[Int]
Section titled “json.get_int(j: Value, key: String) -> Option[Int]”Get an integer value by key. Returns none if key doesn’t exist or value is not an integer.
json.get_int(j, "age")json.get_float(j: Value, key: String) -> Option[Float]
Section titled “json.get_float(j: Value, key: String) -> Option[Float]”Get a float value by key. Returns none if key doesn’t exist or value is not a number.
json.get_float(j, "price")json.get_bool(j: Value, key: String) -> Option[Bool]
Section titled “json.get_bool(j: Value, key: String) -> Option[Bool]”Get a boolean value by key. Returns none if key doesn’t exist or value is not a boolean.
json.get_bool(j, "active")json.get_array(j: Value, key: String) -> Option[List[Value]]
Section titled “json.get_array(j: Value, key: String) -> Option[List[Value]]”Get an array value by key. Returns none if key doesn’t exist or value is not an array.
json.get_array(j, "items")json.as_string(j: Value) -> Option[String]
Section titled “json.as_string(j: Value) -> Option[String]”Extract string from a Json value (without key lookup). Returns none if not a string.
json.as_string(j)json.as_int(j: Value) -> Option[Int]
Section titled “json.as_int(j: Value) -> Option[Int]”Extract integer from a Json value (without key lookup). Returns none if not an integer.
json.as_int(j)json.as_float(j: Value) -> Option[Float]
Section titled “json.as_float(j: Value) -> Option[Float]”Extract float from a Json value (without key lookup). Returns none if not a number.
json.as_float(j)json.as_bool(j: Value) -> Option[Bool]
Section titled “json.as_bool(j: Value) -> Option[Bool]”Extract boolean from a Json value (without key lookup). Returns none if not a boolean.
json.as_bool(j)json.as_array(j: Value) -> Option[List[Value]]
Section titled “json.as_array(j: Value) -> Option[List[Value]]”Extract array from a Json value (without key lookup). Returns none if not an array.
json.as_array(j)json.root() -> JsonPath
Section titled “json.root() -> JsonPath”Create a root JSON path for traversal.
json.root()json.field(path: JsonPath, name: String) -> JsonPath
Section titled “json.field(path: JsonPath, name: String) -> JsonPath”Extend a JSON path with a field name.
json.field(json.root(), "user")json.index(path: JsonPath, i: Int) -> JsonPath
Section titled “json.index(path: JsonPath, i: Int) -> JsonPath”Extend a JSON path with an array index.
json.index(json.field(json.root(), "items"), 0)json.get_path(j: Value, path: JsonPath) -> Option[Value]
Section titled “json.get_path(j: Value, path: JsonPath) -> Option[Value]”Get a value at a JSON path. Returns none if path doesn’t exist.
json.get_path(j, json.field(json.root(), "name"))json.set_path(j: Value, path: JsonPath, value: Value) -> Result[Value, String]
Section titled “json.set_path(j: Value, path: JsonPath, value: Value) -> Result[Value, String]”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"))json.remove_path(j: Value, path: JsonPath) -> Value
Section titled “json.remove_path(j: Value, path: JsonPath) -> Value”Remove a value at a JSON path. Returns the Json with the value removed.
json.remove_path(j, json.field(json.root(), "temp"))json.to_map(j: Value) -> Option[Map[String, String]]
Section titled “json.to_map(j: Value) -> Option[Map[String, String]]”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()