DEV Community

Discussion on: #30daysofelm Day 17: I decoded some JSON!

Collapse
 
bukkfrig profile image
bukkfrig

You still have this wierd function showPlanetOrError which does Json.Decode stuff and Html stuff.

The problem comes from the shape of your JSON. You are using the name of a field to represent what is actually data. You end up having to instruct the machine on how to handle a "Venus" field and a "Mars" field, etc. It might be human-readable, but it's not very machine readable.

Is it within your power to reshape the JSON sent by your API, so it's just a list of planets, and the planets have a name field?

In this Ellie (ellie-app.com/bYpVTc2KhSta1), there are two different versions of the same thing:

  • The first separates the decoding from the rendering, which separates the concerns, but it's very painful to do.
  • The second operates on a different shape of JSON, where it's just a list of planets and the planets have a name field. Now it's very simple!
Collapse
 
kristianpedersen profile image
Kristian Pedersen

Yeah, the original JSON is structured poorly. At first I did want to change it the way you describe, but I decided to keep it.

My thinking was that I might have to deal with other poorly structured JSON APIs in the future, but as you say, it has only made things more difficult.

Your second example is really nice to read - it's this kind of code I want to write. I would have gotten there sooner if I hadn't been so stubborn about the JSON shape, so that's a good lesson.

Also, I gotta say your first example is spectacular - the duplication at line 29 and 30 (List.map2 viewUnnamedPlanet) the nesting at line 78 (bodiesDecoder) are pure poetry. :D