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


    Decode JSON primitives into Elm

    Enrico BuonannoEnrico Buonanno

    The most basic decoders take a JSON primitive such as an Int or a Boolean, and turn it into the corresponding Elm value.



    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


    Instructor: 00:00 Let's go back to the Elm repo and import json.decode. Now, you should take some time to experiment with the primitive decoders. If you have a string that contains 1, that's valid JSON, and it's a number. You could pass that to decodeString with int as a decoder. You can see that this successfully decoded to 1.

    00:24 What if I were to decode this with string? This fails, because 1 is not a valid JSON string. If I change this like this, so I put this into three double quotes, that's a multi-line string in Elm. I can now use double quotes inside that string, so this can now be decoded as the string "1."

    00:43 Let's try one more, true. I should be able to pass that to decodeString using bool as a decoder. This works. If I were to use, say, float as a decoder, as expected, that would fail.