Kaustav
Kaustav

Reputation: 70

Decode nested variable-length JSON in ELM

I'm having trouble wrapping my head around decoding a bit complex JSON response in ELM. The JSON response structure(I have no control over it) doesn't match my application's data tree/model.

What will the decoder code look like for this JSON ?

I'm using NoRedInk's Json.Decode.Pipeline - http://package.elm-lang.org/packages/NoRedInk/elm-decode-pipeline/3.0.0/Json-Decode-Pipeline

My project's Github repo - https://github.com/areai51/my-india-elm

The JSON response - https://data.gov.in/node/85987/datastore/export/json

Code

 type alias Leader =
    { attendance : Float      <------- Logic = (Sessions Attended / Total Sessions) * 100
    , name : String
    , state : String
    }


type alias Model =
    { leaders : WebData (List Leader)
    }


initialModel : Model
initialModel =
    { leaders = RemoteData.Loading
    }

JSON Note that there aren't keys I can directly map to inside the "data" array.

{
   "fields":[   // Definitions for the "data" key
      {
         "id":"a",
         "label":"S.No.",
         "type":"string"
      },
      {
         "id":"b",
         "label":"Division\/Seat No.",
         "type":"string"
      },....
    ],
   "data":[      <------- Leaders
      [
         1,
         3,
         "Smt.  Sonia  Gandhi",      <------- Name
         15,
         12,
         "Uttar Pradesh",      <------- State
         "Rae Barelii",
         20,      <------- Total Sessions (Attendance)
         9      <------- Sessions Attended (Attendance)
      ],
      [
         2,
         15,
         "Shri  Dayanidhi  Maran",      <------- Name
         15,
         12,
         "Tamil Nadu",      <------- State
         "Chennai Central",
         20,      <------- Total Sessions (Attendance)
         7      <------- Sessions Attended (Attendance)
      ],
      [
         3,
         16,
         "Shri  A.  Raja",      <------- Name
         15,
         12,
         "Tamil Nadu ",      <------- State
         "Nilgiris",
         20,      <------- Total Sessions (Attendance)
         16      <------- Sessions Attended (Attendance)
      ],.....
    ]
}

UPDATED SOLUTION

leadersDecoder : Decode.Decoder (List Leader)
leadersDecoder =
    Decode.at [ "data" ] (Decode.list leaderDecoder)


leaderDecoder : Decode.Decoder Leader
leaderDecoder =
    let
        sessionsAttendedDecoder =
            Decode.index 7 Decode.float
                |> Decode.andThen (\total -> attendanceDecoder
                |> Decode.map (\attended -> (attended / total) * 100))
    in
        Decode.map3 Leader
            sessionsAttendedDecoder
            (Decode.index 2 Decode.string)
            (Decode.index 5 Decode.string)

attendanceDecoder : Decode.Decoder Float
attendanceDecoder =
    (Decode.oneOf
        [ Decode.index 8 Decode.float
        , Decode.succeed 0
        ]
    )

Upvotes: 0

Views: 410

Answers (1)

Chad Gilbert
Chad Gilbert

Reputation: 36385

Json.Decode.index can be used to pull a specific index out of an array with the specified decoder. Couple that with andThen and map to do the attendance calculation based on multiple fields:

import Json.Decode exposing (..)

leaderDecoder : Decoder Leader
leaderDecoder =
    let
        sessionsAttendedDecoder =
            index 7 float
                |> andThen (\total -> index 8 float
                |> map (\attended -> (attended / total) * 100))
    in
        map3 Leader
            sessionsAttendedDecoder
            (index 2 string)
            (index 5 string)

Upvotes: 2

Related Questions