Hi folks,
I’m having trouble decoding a JSON object. The API I am talking to has two
properties that should be combined. I want to solve this in the decoding step
and not further down in my code.
This is the object I receive from the API:
{
"video_id": "Mestkever",
"video_extension": "mp4",
"video_thumbnail": "Mestkever",
...
}
Both video_extension
and video_thumbnail
should be joined as strings with a
dot in between to get the full filename. Below is my current implementation. I
got it working, but it won’t scale. Adding more properties means adding more to
toDecoder
. I cannot change the API so I have to solve the problem in our front-end application.
import Json.Decode exposing (Decoder, float, int, string)
import Json.Decode.Pipeline exposing (decode, required, resolve)
type alias Video =
{ id : String
, filename : String
}
decode : Decoder Video
decode =
let
toDecoder : String -> String -> String -> Decoder Video
toDecoder id filename extension =
JD.succeed (Video id (filename ++ "." ++ extension))
in
JD.succeed toDecoder
|> required "video_id" JD.string
|> required "video_thumbnail" JD.string
|> required "video_extension" JD.string
|> JDP.resolve
My gut tells me this should be an easy fix, but can’t figure it out. I’ve created this Ellie to show the implementation above: https://ellie-app.com/7nv8cBBmxRXa1.
How do I change the implementation so that I don’t have a toDecoder
that I have to expand when adding more properties?