squirtle
Types
Represents any JSON value
This type can represent all valid JSON values including objects, arrays, strings, numbers, booleans, and null.
pub type JsonValue {
Null
String(String)
Int(Int)
Bool(Bool)
Float(Float)
Array(List(JsonValue))
Object(dict.Dict(String, JsonValue))
}
Constructors
-
Null -
String(String) -
Int(Int) -
Bool(Bool) -
Float(Float) -
Array(List(JsonValue)) -
A JSON Patch operation Represents one of the six operations defined in RFC 6902:
pub type Patch {
Add(path: String, value: JsonValue)
Remove(path: String)
Replace(path: String, value: JsonValue)
Copy(from: String, path: String)
Move(from: String, path: String)
Test(path: String, value: JsonValue)
}
Constructors
-
Add(path: String, value: JsonValue)Add a value at a path
-
Remove(path: String)Remove a value at a path
-
Replace(path: String, value: JsonValue)Replace a value at a path
-
Copy(from: String, path: String)Copy a value from one path to another
-
Move(from: String, path: String)Move a value from one path to another
-
Test(path: String, value: JsonValue)Test that a value at a path equals an expected value
Values
pub fn json_value_decode(
value: JsonValue,
decoder: decode.Decoder(a),
) -> Result(a, List(decode.DecodeError))
Decode a JsonValue using a custom decoder
This allows you to decode a JsonValue into a specific Gleam type.
Example
import gleam/dynamic/decode
import squirtle
let value = squirtle.Object(...)
squirtle.json_value_decode(value, decode.field("name", decode.string))
// => Ok("John")
pub fn json_value_decoder() -> decode.Decoder(JsonValue)
Returns a decoder for parsing JSON into a JsonValue
This decoder can parse any valid JSON value.
Example
import gleam/json
import squirtle
json.parse("{\"name\": \"John\"}", squirtle.json_value_decoder())
// => Ok(Object(...))
pub fn json_value_parse(
raw: String,
) -> Result(JsonValue, json.DecodeError)
Parse a JSON string into a JsonValue
Example
import squirtle
squirtle.parse("{\"name\": \"John\", \"age\": 30}")
// => Ok(Object(...))
pub fn json_value_to_dynamic(value: JsonValue) -> dynamic.Dynamic
Convert a JsonValue to a Dynamic value
This is useful when you need to use the value with Gleam’s dynamic decoding functions.
Example
import squirtle
let value = squirtle.String("hello")
squirtle.json_value_to_dynamic(value)
pub fn json_value_to_json(value: JsonValue) -> json.Json
Convert a JsonValue to gleam/json’s Json type
This is useful when you need to work with the standard library’s JSON functions.
Example
import squirtle
let value = squirtle.String("hello")
squirtle.json_value_to_json(value)
pub fn json_value_to_string(value: JsonValue) -> String
Convert a JsonValue to a JSON string
Example
import squirtle
let value = squirtle.Object(...)
squirtle.json_value_to_string(value)
// => "{\"name\":\"John\"}"
pub fn parse_patches(
patches: String,
) -> Result(List(Patch), json.DecodeError)
Parse a JSON array of patch operations from a string
Example
import squirtle
let patches_json = "[
{\"op\": \"add\", \"path\": \"/name\", \"value\": \"John\"},
{\"op\": \"remove\", \"path\": \"/age\"}
]"
squirtle.parse_patches(patches_json)
// => Ok([Add("/name", String("John")), Remove("/age")])
pub fn patch(
data: JsonValue,
patches: List(Patch),
) -> Result(JsonValue, String)
Apply a list of patch operations to a JSON document
Applies all patches in order, returning the modified document or an error if any patch fails. All patches must succeed for the operation to succeed.
Example
import squirtle
let assert Ok(doc) = squirtle.parse("{\"name\": \"John\"}")
let patches = [
squirtle.Replace(path: "/name", value: squirtle.String("Jane")),
squirtle.Add(path: "/age", value: squirtle.Int(30)),
]
squirtle.patch(doc, patches)
// => Ok(Object(...))
pub fn patch_decoder() -> decode.Decoder(Patch)
Returns a decoder for parsing JSON into a Patch operation
This decoder parses a single patch operation from JSON according to RFC 6902.
Example
import gleam/json
import squirtle
json.parse("{\"op\": \"add\", \"path\": \"/name\", \"value\": \"John\"}",
squirtle.patch_decoder())
// => Ok(Add("/name", String("John")))
pub fn patch_string(
data: String,
patches: String,
) -> Result(String, String)
Parse and apply JSON patches to a JSON document, both provided as strings
This is a convenience function that combines parsing, patching, and stringifying. Returns the patched document as a JSON string.
Example
import squirtle
let doc = "{\"name\":\"John\",\"age\":30}"
let patches = "[{\"op\":\"replace\",\"path\":\"/name\",\"value\":\"Jane\"}]"
squirtle.patch_string(doc, patches)
// => Ok("{\"name\":\"Jane\",\"age\":30}")