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

    Use `map` to transform data while decoding JSON into Elm

    Enrico BuonannoEnrico Buonanno

    The map function defined for Decoder can be used to perform data transformations while decoding, including changing the type of data. Transforming data while decoding is useful, because decoding is done at the edge of your Elm app, so you can make certain assumptions about the data in your app.

    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:00 One of the important functions in functional programming is map. You're probably familiar with the List.map. This is a function where you have a list of A's, and a function from A to B, and you end up with the list of B's.

    00:15 The function is applied to each A with individual list. There is also Maybe.map, and that is a similar signature you start with the Maybe.A in a function from A to B, and you end up with the Maybe of B. There is a map defined for decoders as well, and the pattern is the same, you start with the decoder of A, and you have a function from A to value, and you end up with the decoder of value.

    00:42 The idea is that, if you can decode some JSON into an A, and you have a way to go from A to this value, then you can decode the JSON into the value. Let's go back to our user decoder to see how this could be useful.

    00:56 Let's see that we want a user's email to ours be lower case. The JSON data that we get is not quite consistent, so we get the occasional applicant's email. Now, as part of decoding JSON, we would like to normalize all the emails to be lower case. What we can do, instead of simply using the string decoder, we can use string and map with the String.toLower function.

    01:21 Now, you can save. You can see that even though my JSON has capital J in the email, but the decoder JSON has lower case still. Let's discuss slightly more interesting example. We have this Boolean to indicate whether the user is a premium user or not.

    01:39 In elm, we can use a more elegant representation of this. We could have a type membership, and we can say that the membership can be standard or premium. Now, in our type definition, rather than having this Boolean, we have a membership of type membership.

    02:02 Now, what we decode a JSON representation of a user, we need to go from Boolean to membership. Here, rather than bool, let me use membership. This would be a new decoder that I'll define just below. The idea is that, I'll start with the normal bool decoder, and then I'll map a function, let me call it two membership and this should be a function from bool to membership.

    02:32 Let me put a definition in let clause. We take a Boolean and in our pattern matching the Boolean. Now, this compiles, and you can see that, in our JSON we have isPremium true and in the decoded elm object, we have membership of premium.

    Discuss

    Discuss