joeybright / json-to-elm / JsonToElm

Take any encoded JSON (as a Json.Encode.JsonValue) and decode it into an Elm value.


type JsonValue
    = JsonInt Basics.Int
    | JsonFloat Basics.Float
    | JsonString String
    | JsonBool Basics.Bool
    | JsonList (List JsonValue)
    | JsonObject (Dict String JsonValue)
    | JsonNull
    | JsonUnknown Json.Encode.Value

A custom type that represents possible JSON values as an Elm type.

decode : Json.Decode.Decoder JsonValue

A decoder for coverting a Json.Encode.JsonValue into a JsonValue.

If you run decode on the following JSON:

{
    "name": "John",
    "age": 10,
    "money": 10.2,
    "isTired": "true",
    "objects": [ "wallet", "phone", "keys" ]
}

You will get the following JsonValue result:

JsonObject
    (Dict.fromList
        [ ( "name", JsonString "John" )
        , ( "age", JsonInt 10 )
        , ( "money", JsonFloat 10.2 )
        , ( "isTired", JsonBool True )
        , ( "object"
          , JsonList
                [ JsonString "wallet"
                , JsonString "phone"
                , JsonString "keys"
                ]
          )
        ]
    )

You can then manipulate the returned Elm type however you'd like in your program!