Autoplay

    Decode JSON into Elm: Course Overview

    Enrico BuonannoEnrico Buonanno

    When developing real world applications in Elm you will encounter the problem of decoding JSON data from an API.

    Elms static types need data converted from JSON to something Elm can understand. Elm exposes decoders for us to do just that.

    In this course, you will learn how to decode individual values, simple JSON objects, lists, and nested objects. You will also learn how to handle nullable and optional fields in Elm as well as returning data or an error object when requests are bad.

    After taking this course, you will have a solid grasp on converting JSON into Elm so you can pull real world data into your application

    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 As soon as you start writing real-world applications in Elm, one of the challenges that you'll encounter is that of decoding JSON into Elm. Elm has this nice static type system that gives us a lot of compile time guarantees. In a sense, it could not be more different from JSON.

    00:19 It's inevitable that it takes a bit of work to translate between the two. Elm uses decoders to translate from JSON into Elm and encoders to translate from Elm into JSON. This course covers decoders in depth. In all the examples, we have some JSON as a string, the corresponding Elm type that we want to translate to, and a decoder, which is like a specification on how to perform the translation.

    00:45 We'll then run the decoder and print the decoded object. This approach gives you instant feedback on whether your decoder is working as intended. We look at the primitive decoders for decoding single values like ints and bools. We'll then look at more complex decoders that can decode objects, lists, lists of objects, nested objects, and so on.

    01:07 Elm doesn't have null values and uses the maybe type instead. We look at handling nullable JSON fields as well as optional fields. A very powerful feature of Elm's type system is union types, which have no equivalent in JSON. We look at examples of how to convert a Boolean to a union type, a string to a union type, and finally, a more complex example of how an API that can return either some data or an error object can be decoded into a result.

    01:37 We'll look at the APIs provided by JSON.decode, which is part of the core library and also at some other useful packages, namely JSON Extra, which contains a number of useful functions such as date for decoding dates, form result for converting between a result and a decoder, apply to decode large objects, and more.

    02:01 We'll also look at decoding objects with elm-decode-pipeline, another popular package. Finally, we'll look at decoding objects as dictionaries and at decoding JSON with a recursive structure such as category trees or navigation trees.

    02:16 By the end of this course, you will have seen plenty of examples and will have gained full confidence to write the decoders you need to consume JSON data required in your projects.

    Discuss

    Discuss