In this post we will discuss how to turn nested JSON into Elm data types. This post uses a thin Elm.Http
wrapper. Using this library helps us concentrate on contructing the necessary Decoders. We will also contruct a more complex query in order to explain how to contruct complex data types. The following query returns JSON for the first two team members of FracturedAtlas.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
|
Swapping out our query will return the following JSON.
1
|
|
The following part took me a long time to grok. The issues I had difficulty resolving are: 1. How do I grab the list of nodes? 1. How to I change my stringified JSON output into Elm data types?
The rest of this post tackles these two issues.
The requiredAt
function from Elm Decode Pipeline solves the first problem.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
|
Let’s construct our Elm datatypes based on the JSON response.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
|
Now to handle the update function.
1 2 3 4 5 6 7 8 9 10 11 12 |
|
I spent numerous hours figuring out how to map the Elm data types to the decoders. In Part 3, we will learn how to display the User
data type in the browser.