Safely Decode JSON

Prior to this point, you’ve been able to stay safely within the confines of Elm’s magical world of static types. However, you’re going to run into an interesting dilemma if you want to accept an arbitrary JSON payload from a server. Elm doesn’t have a JSON.parse function like JavaScript because it can’t dynamically create records like JavaScript can create objects. In this section, you’re going to learn about JSON decoders, why they’re important, and how to use them to safely convert JSON into a static type Elm can use.

Understand the Problem

To understand why you need JSON decoders, let’s look at a couple of example JSON payloads you could use with your application. Visit https://programming-elm.com/feed/1 in your browser. ...

Get Programming Elm now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.