Join egghead, unlock knowledge.

Want more egghead?

This lesson is for members. Join us? Get access to all 3,000+ tutorials + a community with expert developers around the world.

Unlock This Lesson

Already subscribed? Sign In

Autoplay

    Decode JSON Objects into Elm Records Using `field` and `mapX`

    Enrico BuonannoEnrico Buonanno

    The most natural translation of a JSON data object is into an Elm record. For simple one-to-one translations, you use the field decoder to extract values from single fields, and the mapX functions to combine them in the creation of a record.

    elmElm
    Code

    Code

    Become a Member to view code

    You must be a Member to view code

    Access all courses and lessons, track your progress, gain confidence and expertise.

    Become a Member
    and unlock code for this lesson
    Transcript

    Transcript

    Instructor: 00:01 Let's look at decoding objects into records in more detail. Notice how we're using the field function for each object's field. Let me demonstrate how a field would work in isolation. Instead of using userDecoder, I'll just use field ID with string as a decoder.

    00:19 If I save that, notice that we get OK, 123. When you use a field with an object, it only extracts the single field that is named. If I change this to email, not surprisingly, it extracts just the email. To decode an object, the first step would be to create a field decoder for every field. The second step is to use this mapx function. Here it's map4, because we're using four fields.

    00:49 How does that work? We've seen an example of using the normal map function right here. You can see that this takes a decoder and a unary function. The idea here is that you get a decoder, in this case to bool. When you run this, you run the decoder to obtain a bool, and then you pass that bool to membership function.

    01:10 Of course, if decoding fails, then you wouldn't have a bool, and you wouldn't be able to pass that value. Then the decoder would fail, as a whole. When you have the mapx functions, these adjust variations of map that take not a unary function, but a function with more arguments.

    01:26 In this case, the function is user. What this user actually means is a function that takes four arguments -- let's call them A, B, C, D -- and then returns user with A, B, C, D. Keep in mind that whenever you define a type alias, as we have with user, it also creates a shorthand function that we can use like this, passing it the value for various fields.

    01:52 Now, if you look at map4, it looks like this. We have a function that takes four arguments. Here, we have the first argument, but it's wrapped in a decoder. Map4 will eventually run this decoder.

    02:04 If the decoding succeeds, it will use the resulting int for the A parameter of this function here. Then, it will look at the second field. It will try to decode it. If the decoding succeeds, this will become the second parameter given to the function, and so on.

    02:19 Let me just revert to calling the user function in a point-free way. What this goes to show is that map4 doesn't have anything specifically linked with records or with objects. You don't need to use the mapx functions with a record name, but you could use them with any function.

    02:35 For example, let me create another user decoder -- let me call it userDecoder2 -- that uses map2. We'll have a binary function here, A, B. Let's look at the two fields. We'll take the ID and the email, and then what I want to return is just the concatenation of these two fields.

    03:11 What this decoder is saying is extract the ID field, extract the email field, and concatenate the two. This is how we will decode a user. Let me now try decoding the user with the userDecoder2. As you can see, we get 123, which is the ID concatenated with the email.

    Discuss

    Discuss