Last active
September 13, 2018 14:05
-
-
Save hipertracker/36afd3fa89c1f446cddd0a1fd1d53b6b to your computer and use it in GitHub Desktop.
Decoding nested JSON string
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Nested exposing (api) | |
import Json.Decode exposing (at, int, string, list, decodeString, Decoder) | |
import Json.Decode.Pipeline exposing (decode, required, optional, hardcoded) | |
data : String | |
data = | |
""" | |
{"languages": | |
[ | |
{"id": 6, "name": "en", "label": "English"}, | |
{"id": 7, "name": "eo", "label": "Esperanto"} | |
] | |
} | |
""" | |
type alias Language = | |
{ id : Int | |
, name : String | |
, label : String | |
} | |
type alias Languages = | |
{ languages : List Language } | |
languageDecoder : Decoder Language | |
languageDecoder = | |
decode | |
Language | |
|> required "id" int | |
|> required "name" string | |
|> required "label" string | |
languagesDecoder : Decoder Languages | |
languagesDecoder = | |
decode | |
Languages | |
|> required "languages" (list languageDecoder) | |
api : Result String Languages | |
api = | |
decodeString (languagesDecoder) data |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment