I was just starting to study Elma, and I ended up at the checkpoint. Seek help from this amazing community.
I want to decode a nested json and inject a specific nested value into an elm record.
The json source is as follows:
{
"id": 672761,
"modified": "2018-02-12T00:53:04",
"slug": "Vivamus sagittis lacus vel augue laoreet rutrum faucibus dolor auctor.",
"type": "post",
"link": "https://awesomelinkhere.com",
"title": {
"rendered": "Sed posuere consectetur est at lobortis."
},
"content": {
"rendered": "Nulla vitae elit libero, a pharetra augue.",
},
"excerpt": {
"rendered": "Donec sed odio dui.",
}
}
and I want to divide title.renderedand content.renderedinto the field in my model, the model looks like this:
type alias Post =
{ id : Int
, published : String
, title : String
, link : String
, slug : String
, excerpt : String
}
my naive decoder looks like this
postDecoder : Decoder Post
postDecoder =
Decode.map6 Post
(field "id" Decode.int)
(field "modified" Decode.string)
(field "title" Decode.string)
(field "link" Decode.string)
(field "slug" Decode.string)
(field "excerpt" Decode.string)
source
share