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 large JSON Objects into Elm with `json-extra`

    Enrico BuonannoEnrico Buonanno

    The mapX functions go up to map8, so if you want to construct records with more than 8 fields, you need to use a different approach. One such approach is to use the |: operator in Json.Decode.Extra.

    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 One of the things that you may have noticed and that is slightly annoying is that every time that you add a field to your record, then you need to change which mapx function you use. We started using map2, and then, as we've added more fields, we're now using map6.

    00:16 The other thing you might wonder is, "How many of these functions exist? If I have a record with 40 values, then would I use map40?" The answer is that the limit is eight. Json.decode has mapx functions up to map8. If you need a construct a record that combines more than eight decoders, you need to use a different library.

    00:40 One solution is to use the json.extra package, which we've already imported, and to additionally expose this operator with the |:, which you should read apply or nmap. Then, rather than using map6, you use succeed. This effectively wraps your function that creates a user into a decoder that always succeeds.

    01:06 Then you apply the other decoders with this operator |: . Let me save. You can see this works just as before. Furthermore, if you add the new field now, you don't need to change from, say, map6 to map7. Secondly, you no longer have this ceiling of eight fields maximum.

    Discuss

    Discuss