tjb1982 tjb1982 - 10 months ago 71
JSON Question

Elm decoding unknown json structure

I've just started working with Elm to do some front-end prototyping using a Rest API I'm working on. In general, the API returns "reasonable" data structures that can be decoded because the keys and value-types are well-known, but several resource types return a

entry that just has raw json that has no predetermined structure.

Everything I've read so far seems to assume you know the structure of the data you're decoding, whereas in plain js it's relatively easy to loop over the keys and reflect on the types in order to determine how they should be handled at runtime. I'm not seeing a clear path toward handling this kind of data in Elm yet.


"data": {
"bar": [{"baz":123}, "quux"]

I'd like to know if it is currently possible to parse the value of the
entry with something akin to

function go(obj)
for key in keys(foo)
if foo[key] is an object
else if foo[key] is an array
map(go, foo[k])


  1. Is it currently possible to handle unknown, possibly deeply nested and heterogeneous json data in Elm?

  2. If so, can you give me the key concept or high level intuition on how the author(s) intended data like this to be decoded?

Answer Source

Yes, it is possible to write a general purpose decoder. You can first define a union type that holds all possible Json types:

type JsVal
  = JsString String
  | JsInt Int
  | JsFloat Float
  | JsArray (List JsVal)
  | JsObject (Dict String JsVal)
  | JsNull

And now you can use Json.Decode.oneOf to try every possibility.

import Json.Decode as D exposing (Decoder)
import Dict exposing (Dict)

jsValDecoder : Decoder JsVal
jsValDecoder =
    [ D.string |> D.andThen (D.succeed << JsString)
    , |> D.andThen (D.succeed << JsInt)
    , D.float |> D.andThen (D.succeed << JsFloat)
    , D.list (D.lazy (\_ -> jsValDecoder)) |> D.andThen (D.succeed << JsArray)
    , D.dict (D.lazy (\_ -> jsValDecoder)) |> D.andThen (D.succeed << JsObject)
    , D.null JsNull

Json.Decode.lazy is necessary for the JsArray and JsObject constructors because they are defined recursively.

This structure should handle anything you throw at it, and it will be up to the rest of your program to decide what to do with such a flexible type.