All Projects → chrisbuttery → Elm Simple Json Decoding

chrisbuttery / Elm Simple Json Decoding

A playground of basic examples decoding a JSON object

Programming Languages

elm
856 projects

Elm Simple JSON Decoding

A playground for decoding a JSON object in elm.

One thing that threw me with decoding a JSON response was that you have to know what the response is before you can decode it.

To my knowledge, unlike JavaScript you can't seem to blurt out the complete response of a Http request to a console or whatever - and then decide what parts you need.

It's no biggie. If you're unsure of the structure of your response, try using something like Postman or Paw.

The following examples are using the same endpoint yet decoding different levels of data in the responses. I've also included an example on handling a Http.Error.

Response

https://api.myjson.com/bins/yws2

{
  "title": "This is an amazing title",
  "data": [
    {
      "id": 1,
      "name": "foo"
    },
    {
      "id": 2,
      "name": "bar"
    },
    {
      "id": 3,
      "name": "baz"
    }
  ],
  "obj": {
    "title": "I'm a nested object"
  },
  "members": [
    {
      "id": 4,
      "name": "garply",
      "profile": {
        "avatar": "some_path_to_garply"
      }
    },
    {
      "id": 5,
      "name": "waldo",
      "profile": {
        "avatar": "some_path_to_waldo"
      }
    },
    {
      "id": 6,
      "name": "fred",
      "profile": {
        "avatar": "some_path_to_fred"
      }
    }
  ]
}

chrisbuttery.com  ·  GitHub @chrisbuttery  ·  Twitter @buttahz  ·  elm-lang slack @butters

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].