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 Error/Success Data into Elm Union Types

    Enrico BuonannoEnrico Buonanno

    Sometimes your JSON may have completely different structure; for example, an HTTP API may reply with some data, or with an "error" object containing details about why the data could not be delivered. You can decode JSON with such variable structure with the oneOf function, which takes a list of decoders and tries running them against the given JSON. For further discussion on how to model server errors I recommend you also watch this lesson on handling server side validation errors



    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:01 Imagine that you have an HTTP API that can send you a representation for errors. For example, you might have an HTTP request to log in a user. If the login succeeds, you get an object with a single field called data, and this will have all the data associated with the user that's logging in.

    00:23 Let me call this sample success. This is the sort of data that you will get if the login is successful. Let me define a sample failure. This would also be a JSON object with a single field called error. This will have a property called message.

    00:54 This could be, for example, "wrong password." If the user tries to log in with the wrong password, you would get a message like this from your API. If the login is successful, then the message would look like this. How could we write a decoder that can handle these sort of messages?

    01:18 Firstly, how can we represent such different messages with a single type? We could use the result type, result. If we have an error, we could have an object that represents this data. Let's assume that an error always has a message, the only interesting piece of information is the string that identifies the error.

    01:38 The first type parameter for the result would be string. If the request is successful, then we get a user object. This is how we could model the response from our server. What we need to write is then a decoder for such a type.

    01:55 Let's write a response decoder that has this type. How could we define this? When you have a number of different shapes that your JSON can take, then there's this function called oneOf. This takes a list of decoders, and it will try all the different decoders.

    02:14 Let's write a decoder for success. In the successful response, we're expecting a field called the data, and the data inside that field is the data for a user. Let's write user, and let me rename this user decoder to simply user. What this is saying is try to look for a field called data and decode this as a user.

    02:40 Because we want a result of string of user, let me map this with "OK." This will be a successful result. In the case of failure, we're looking for an error message. Let me use at with error and message. This would extract this string, we need to say this is a string, and then we map this to error.

    03:11 Let me go to the main function. Let's try to decode the sample success with our decoder that's called response. Let me save. Notice that now, we have a nested result. We have "OK" and "OK." The first results tells you whether decoding JSON was successful. The second result tells you whether the response was a successful response.

    03:38 Indeed, we get successful decoding and a successful response with the data for our user. Now, let me try decoding the sample failure. Again, the decoding has gone OK, but the response is a failure response, with a payload "wrong password."