Reputation: 2279
I have this data using http://jsonapi.org/ format:
{
"data": [
{
"type": "prospect",
"id": "1",
"attributes": {
"provider_user_id": "1",
"provider": "facebook",
"name": "Julia",
"invitation_id": 25
}
},
{
"type": "prospect",
"id": "2",
"attributes": {
"provider_user_id": "2",
"provider": "facebook",
"name": "Sam",
"invitation_id": 23
}
}
]
}
I have my models like:
type alias Model = {
id: Int,
invitation: Int,
name: String,
provider: String,
provider_user_id: Int
}
type alias Collection = List Model
I want to decode the json into a Collection, but don't know how.
fetchAll: Effects Actions.Action
fetchAll =
Http.get decoder (Http.url prospectsUrl [])
|> Task.toResult
|> Task.map Actions.FetchSuccess
|> Effects.task
decoder: Json.Decode.Decoder Collection
decoder =
?
How do I implement decoder? Thanks
Upvotes: 18
Views: 6727
Reputation: 6366
N.B. Json.Decode docs
Try this:
import Json.Decode as Decode exposing (Decoder)
import String
-- <SNIP>
stringToInt : Decoder String -> Decoder Int
stringToInt d =
Decode.customDecoder d String.toInt
decoder : Decoder Model
decoder =
Decode.map5 Model
(Decode.field "id" Decode.string |> stringToInt )
(Decode.at ["attributes", "invitation_id"] Decode.int)
(Decode.at ["attributes", "name"] Decode.string)
(Decode.at ["attributes", "provider"] Decode.string)
(Decode.at ["attributes", "provider_user_id"] Decode.string |> stringToInt)
decoderColl : Decoder Collection
decoderColl =
Decode.map identity
(Decode.field "data" (Decode.list decoder))
The tricky part is using stringToInt
to turn string fields into integers. I've followed the API example in terms of what is an int and what is a string. We luck out a little that String.toInt
returns a Result
as expected by customDecoder
but there's enough flexibility that you can get a little more sophisticated and accept both. Normally you'd use map
for this sort of thing; customDecoder
is essentially map
for functions that can fail.
The other trick was to use Decode.at
to get inside the attributes
child object.
Upvotes: 25