DEV Community

Kristian Pedersen
Kristian Pedersen

Posted on

#30daysofelm Day 15: Struggling with JSON :|

This is day 15 of my 30 day Elm challenge

Today I wanted to do some stuff with the JSON from day 9: Astronomy data from Python in Elm

There's no finished code today, and I feel like I'm fumbling around blindly to be honest.

I've learned a couple of new concepts though, so this isn't a failure at all. :)

Here is today's unfinished non-working code if you're interested: https://github.com/kristianpedersen/30-days-of-elm/tree/main/015-json

In one terminal, run pyhton3 app.py, and in another one, navigate to templates and run elm-live src/Main.elm (npm install -g elm-live first)

0. Simple example

Here's a very basic example.

API:

{ hiMessage: "hi" }
Enter fullscreen mode Exit fullscreen mode
hi : Decoder String
hi =
  field "hiMessage" string
Enter fullscreen mode Exit fullscreen mode

1. Describing the data I want

The JSON I want looks like this:

{"Jupiter":{"lightMinutes":49.561547588282494,"xyz":[-15.160997437594864,35.36776769042324,86.01557267383876]},"Mars":{"lightMinutes":7.056627943382205,"xyz":[-10.802377084003577,-7.957103044154623,0.0]},"Mercury":{"lightMinutes":11.873735464944051,"xyz":[-15.39294493449285,5.8815766045335,-15.430849180096907]},"Neptune":{"lightMinutes":251.12126006612468,"xyz":[-200.051138076174,-188.4930229293946,-390.39907178706875]},"Pluto":{"lightMinutes":292.0545551594518,"xyz":[-487.1954814422351,-265.82244414880427,-17.713974776837258]},"Saturn":{"lightMinutes":90.34545293522373,"xyz":[10.90385529195623,67.16589122515714,157.720111144322]},"Sol":{"lightMinutes":8.178862544580388,"xyz":[-3.3688535760154714,10.267169613495522,-11.182551853223776]},"Uranus":{"lightMinutes":159.9896536380283,"xyz":[-300.96106486799965,44.176269302014866,0.0]},"Venus":{"lightMinutes":12.801164839345946,"xyz":[-12.926388560134972,-6.636737189572844,19.52518546405315]}}
Enter fullscreen mode Exit fullscreen mode

I could probably re-write the JSON from Python, but I guess it's nice to be able to describe all kinds of JSON.

type alias Planets =
    List Planet


type alias Planet =
    { planetName : PlanetInfo }


type alias PlanetInfo =
    { lightMinutes : Float
    , xyz : List Float
    }
Enter fullscreen mode Exit fullscreen mode

Something like this would be the starting point, but this is as far as I've come today.

I'm not sure if these type aliases will work, I can't get any text to display, and I can't figure out how to debug it, so today's upload is just the repo.

I'll celebrate the new year instead, get a good night's sleep, and hopefully connect the pieces next year. ;)

Top comments (2)

Collapse
 
wolfadex profile image
Wolfgang Schuster • Edited

Sorry JSON is causing you such troubles. Maybe a few pointers to help guide you towards an answer when you come back to this day, assuming you do.

Some suggestions on changes to your types. Looking at your data

{
  "Jupiter": {
    "lightMinutes": 49.561547588282494,
    "xyz": [
      -15.160997437594864,
      35.36776769042324,
      86.01557267383876
    ]
  },
 ...
}
Enter fullscreen mode Exit fullscreen mode

To me, this looks more like Dict String PlanetInfo since you're only going to have 1 instance for each planet. List Planet would allow multiples of any planet.

Next, your PlanetInfo looks close, but I think you'd be better suited to have the "xyz" key converted to something like

type alias Point =
    { x : Float
    , y : Float
    , z : Float
    }
Enter fullscreen mode Exit fullscreen mode

This brings your types to something like

type alias Planets =
    Dict String PlanetInfo

type alias PlanetInfo =
    { lightMinutes : Float
    , position : Point
    }

type alias Point =
    { x : Float
    , y : Float
    , z : Float
    }
Enter fullscreen mode Exit fullscreen mode

I hope this gives you a much better starting point. Happy to help more if you're interested.


And happy new year!

Collapse
 
kristianpedersen profile image
Kristian Pedersen

Thank you so much, and happy new year!

I had overlooked dicts, and there are a couple of other things I'm not clear on, so elm/core is on today's reading list. :D

Your input seems like a very good starting point for today. Yesterday was still a step forward, so I'm happy with my progress. Again, thanks for your help!