Skip to content

Instantly share code, notes, and snippets.

@arbaaz
Created August 13, 2017 17:18
Show Gist options
  • Save arbaaz/0bda66dde30dadcca361c98490df8960 to your computer and use it in GitHub Desktop.
Save arbaaz/0bda66dde30dadcca361c98490df8960 to your computer and use it in GitHub Desktop.
JSON decode
-- json : String
-- json =
-- """
-- {
-- "kind": "Listing",
-- "data": {
-- "children": [
-- {"data":{"url":"http://www.example.com", "title":"hello"}},
-- {"data":{"url": "http://www.example.com", "title":"world"}}
-- ]
-- }
-- }
-- """
type alias Post =
{ url : String
, title : String
}
type alias PostList =
List Post
postDecoder : Decoder Post
postDecoder =
JD.map2 Post
(field "url" string)
(field "title" string)
-- postsDecoder : Decoder PostList
-- postsDecoder =
-- at [ "data", "children" ] <|
-- JD.list <|
-- at [ "data" ] postDecoder
postsDecoder : Decoder PostList
postsDecoder =
at [ "data" ] postDecoder
|> JD.list
|> at [ "data", "children" ]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment