Skip to content

json

import json
FunctionSignatureDescription
parse(String) -> Result[Value, String]Parse a JSON string into a Value.
stringify(Value) -> StringConvert 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) -> ValueCreate a Json string value.
from_int(Int) -> ValueCreate a Json integer value.
from_bool(Bool) -> ValueCreate a Json boolean value.
null() -> ValueCreate a Json null value.
array(List[Value]) -> ValueCreate a Json array from a list of Json values.
from_float(Float) -> ValueCreate a Json float value.
stringify_pretty(Value) -> StringConvert a Json value to a pretty-printed JSON string with indentation.
object(List[(String, Value)]) -> ValueCreate 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() -> JsonPathCreate a root JSON path for traversal.
field(JsonPath, String) -> JsonPathExtend a JSON path with a field name.
index(JsonPath, Int) -> JsonPathExtend 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) -> ValueRemove a value at a JSON path. Returns the Json with the value removed.

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\"}")

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")

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)

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)

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)

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)

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()