simplejson
SimpleJSON
Basic JSON library for Gleam. To be used for simple conversion from string to a basic JSON structure and to then output that as a string again.
Values
pub fn apply_pointer(
json: jsonvalue.JsonValue,
pointer: String,
) -> Result(jsonvalue.JsonValue, Nil)
Takes the provided JSONPointer and returns the JsonValue that the pointer refers to or Error(Nil) if it fails for some reason
This executes based on RFC6901 (https://www.rfc-editor.org/rfc/rfc6901)
Examples
let assert Ok(json) = simplejson.parse("{\"a\":[1,{\"b\":2},3]}")
simplejson.apply_pointer(json, "/a/1/b")
// -> Ok(JsonNumber(Some(2), None, Some("2")))
pub fn jsonpath(
json: jsonvalue.JsonValue,
jsonpath: String,
) -> Result(jsonvalue.JsonValue, jsonvalue.JsonPathError)
Simple jsonpath style querying method
A simple . separated list of path elements to take
- names are as-is
- indexes are prefixes by #
- consecutive separators are ignored
- e.g. key1.#3…nextkey
Examples
let assert Ok(json) = simplejson.parse("{\"a\":[1,2,{\"b\":123}]}")
simplejson.jsonpath(json, "a.#2.b")
// -> Ok(JsonNumber(Some(123), None, Some("123")))
pub fn parse(
json: String,
) -> Result(jsonvalue.JsonValue, jsonvalue.ParseError)
Parse a given string into a JsonValue Result. Or return Error if unable.
Thie returns a useful description
of the parse failure utilising the ParseError types.
The error will either be UnexpectedEnd or a specific reason
containing the character/value that failed to parse, a context which
contains the surrounding up to 10 characters and the character
index of the failure point
Examples
parse("{\"a\":123,\"b\":[true, false]}")
// -> Ok(JsonObject(dict.from_list([#("a", JsonNumber(Some(123), None, Some("123"))), #("b", JsonArray([JsonBool(True), JsonBool(False)]))])))
parse("[1,2,3]")
// -> Ok(JsonArray([JsonNumber(Some(1), None, Some("1")), JsonNumber(Some(2), None, Some("2")), JsonNumber(Some(3), None, Some("3"))]))
parse("[1,2,3,]")
// -> Error(UnexpectedCharacter("]", ",2,3,]", 7))
pub fn query(
json: jsonvalue.JsonValue,
path: List(@internal Segment),
) -> jsonvalue.JsonValue
Takes the provided path and json and returns a Json Array of results
This executes based on RFC9535 (https://www.rfc-editor.org/rfc/rfc9535)
Examples
let assert Ok(path) = simplejson.to_path("$[1]")
let assert Ok(json) = simplejson.parse("[1,2,3]")
simplejson.to_string(simplejson.query(json, path))
// -> [2]
pub fn to_path(
str: String,
) -> Result(List(@internal Segment), jsonvalue.JsonPathError)
Converts the passed string into a query type to be used in the query function
This parses based on RFC9535 (https://www.rfc-editor.org/rfc/rfc9535)
Examples
let assert Ok(path) = simplejson.to_path("$[1]")
// -> [Child([Index(1)])]
pub fn to_string(json: jsonvalue.JsonValue) -> String
Convert a given JsonValue into a String
Examples
to_string(JsonArray([JsonNumber(Some(1), None, Some("1")), JsonNumber(Some(2), None, Some("2")), JsonNumber(Some(3), None, Some("3"))]))
// -> "[1,2,3]"