Search code examples
jsonelm

Decode a JSON tuple to Elm tuple


My JSON looks like the following

{ "resp":
    [ [1, "things"]
    , [2, "more things"]
    , [3, "even more things"]
    ]
}

the problem is that I can't parse the JSON tuples into Elm tuples:

decodeThings : Decoder (List (Int, String))
decodeThings = field "resp" <| list <| map2 (,) int string

It compiles, but when ran, it throws

BadPayload "Expecting an Int at _.resp[2] but instead got [3, \"even more things\"]

For some reason it reads [3, "even more things"] as only one thing and not as tuple in JSON format.
How can I parse my JSON into a List (Int, String)?


Solution

  • You need a decoder which turns a javascript array of size two into an Elm tuple of size two. Here is an example decoder:

    arrayAsTuple2 : Decoder a -> Decoder b -> Decoder (a, b)
    arrayAsTuple2 a b =
        index 0 a
            |> andThen (\aVal -> index 1 b
            |> andThen (\bVal -> Json.Decode.succeed (aVal, bVal)))
    

    You can then amend your original example as follows:

    decodeThings : Decoder (List (Int, String))
    decodeThings = field "resp" <| list <| arrayAsTuple2 int string
    

    (Note that my example decoder does not fail if there are more than two elements, but it should get you pointed in the right direction)