Skip to content

Latest commit

 

History

History
235 lines (147 loc) · 4.48 KB

json.md

File metadata and controls

235 lines (147 loc) · 4.48 KB

json

JSON parsing and querying. import json.

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

Convert a Value to a JSON string.

json.stringify(person.encode())

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]

Get all keys of a JSON object as a list of strings.

json.keys(j)

json.from_string(s: String) -> Value

Create a Json string value.

json.from_string("hello")

json.from_int(n: Int) -> Value

Create a Json integer value.

json.from_int(42)

json.from_bool(b: Bool) -> Value

Create a Json boolean value.

json.from_bool(true)

json.null() -> Value

Create a Json null value.

json.null()

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

Create a Json float value.

json.from_float(3.14)

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

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]

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]

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]

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]

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]]

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]

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]

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]

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]

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]]

Extract array from a Json value (without key lookup). Returns none if not an array.

json.as_array(j)

json.root() -> JsonPath

Create a root JSON path for traversal.

json.root()

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

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]

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]

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

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]]

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