Json.Decode.Dynamic

JSON

Walk unknown or heterogeneous JSON without a full typed Decoder tree.

Errors are Result String a. Prefer Json.Decode when the shape is known up front; use this for webhooks, jsonb blobs, and config.

import Json.Decode as Decode
import Json.Decode.Dynamic as Dynamic

name : Decode.Decoder String
name =
    Dynamic.decoder
        (\dyn ->
            Dynamic.field "name" dyn
                |> Dynamic.andThen Dynamic.string
        )

Types

Dynamic

type Dynamic = Dynamic

Untyped JSON term for dynamic probes. Bridge with fromValue / toValue and Json.Decode.value.

Values

fromValue

fromValue : Value -> Dynamic

Decode a stored Json.Value into a walkable Dynamic.

toValue

toValue : Dynamic -> Value

Encode a Dynamic back to Json.Value.

string

string : Dynamic -> Result String String

Expect a JSON string.

int

int : Dynamic -> Result String Int

Expect a JSON integer.

bool

bool : Dynamic -> Result String Bool

Expect a JSON boolean.

float

float : Dynamic -> Result String Float

Expect a JSON number as Float.

null

null : Dynamic -> Result String ()

Expect JSON null.

field

field : String -> Dynamic -> Result String Dynamic

Read an object field as Dynamic.

index

index : Int -> Dynamic -> Result String Dynamic

Read an array element as Dynamic.

list

list : (Dynamic -> Result String a) -> Dynamic -> Result String (List a)

Decode every element of a JSON array.

oneOf

oneOf : List (Dynamic -> Result String a) -> Dynamic -> Result String a

Try each reader until one succeeds.

map

map : (a -> b) -> Result String a -> Result String b

Transform a successful dynamic result.

andThen

andThen : (a -> Result String b) -> Result String a -> Result String b

Chain dynamic results.

decoder

decoder : (Dynamic -> Result String a) -> Decoder a

Lift a dynamic reader into a typed Json.Decode.Decoder.