JSON Examples

894 views
Skip to first unread message

Zachary Kessin

unread,
Feb 18, 2015, 7:09:54 AM2/18/15
to elm-d...@googlegroups.com
Are there any full examples of how to work with the JSON library, just having the function signatures is not enough for me to quite figure stuff out.

Elm really needs more Ajax web app type examples

--Zach
--
Zachary Kessin
Mostly Erlang Podcast
Skype: zachkessin
Twitter: @zkessin

Dave Smith

unread,
Feb 22, 2015, 5:30:21 PM2/22/15
to elm-d...@googlegroups.com
+1

Laurence Roberts

unread,
Feb 23, 2015, 12:41:24 PM2/23/15
to elm-d...@googlegroups.com
It's probably best to open an issue on the elm website github repo to make sure this doesn't get lost - https://github.com/elm-lang/elm-lang.org

Alexander Noriega

unread,
Feb 23, 2015, 8:15:33 PM2/23/15
to elm-d...@googlegroups.com
Hey. 

Here's a port of one of the examples I used in the old-and-now-deprecated lib: http://share-elm.com/sprout/54ebc9aae4b09711f39c2d6e

However, do try to figure out the lib from the function signatures. They should tell you everything you need to know – with the exception of `String` which is a useless type when it comes to documentation, but in e.g. the Decode module you know it's all about JSON decoding, so you can assume `String` means "any string with raw JSON". 

And more importantly, the types tell you more about the possibilities of usage than any given anecdotal example.

See the first thing you have in the docs:

decodeString : Decoder a -> String -> Result String a

Ok, the function needs a `Decoder a` and a `String`. That's easy, we just need a `Decoder` and a `String`.

For the `String` we can just make up any garbage just to see what happens. But where to get a `Decoder` from? Well, there better be a function somewhere that produces one! And indeed in the docs you see plenty of them in the "Primitives" section.

So let's try it with `int`:

import Graphics.Element (Element, flow, down)
import Text as T
import Json.Decode (..)

x = decodeString int "xczcxzcz"

main = T.asText x

You get Err ("Unexpected token x") as a result.

OK so it couldn't decode the string "xczcxzcz" into an int. Makes sense!

How about decodeString int "42"?

You get Ok 42

And so on.

UEHARA Junji

unread,
Feb 23, 2015, 11:25:15 PM2/23/15
to elm-d...@googlegroups.com
I read Json.Decode document recently, and I generally agree 'How type is very eloquent, it is!'. Almost all of information is naturally derived from type information without source. I impressed.

But simultaneously I think more example is needed.
For example, how Can I decode object which have more than 8 fields by using the API? It is not obvious I think.

This is the way I found, and another examples:
(Is there better way?)



--
You received this message because you are subscribed to the Google Groups "Elm Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elm-discuss...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
UEHARA Junji

Arian van Putten

unread,
Feb 24, 2015, 8:53:27 AM2/24/15
to elm-d...@googlegroups.com
To decode more than 8 objects you would need to introduce something like  <~ and ~ for decoders. In haskell this is called the Applicative typeclass. I Don't think it hurts to introduce something similar in elm as this kind of  map1,map2,map3,map4,map5,mapn structure happens in a lot of libraries.

Anyhow you can do the same chaining with the `andThen` function in the Decode library but it's kind of messy:

    map9 f m1 m2 m3 m4 m5 m6 m7 m8 m9 =
      andThen m1 (\x1 ->
      andThen m2 (\x2 ->
      andThen m4 (\x4 ->
      andThen m5 (\x5 ->
      andThen m6 (\x6 ->
      andThen m7 (\x7 ->
      andThen m8 (\x8 ->
      andThen m9 (\x9 -> suceed (f x2 x2 x3 x4 x5 x6 x7 x9)
      ))))))))

    
I think we could just introduce the `<~` and the `~` operators in Json.Decode . it  would make code a lot more elegant:
  
    map9 f m1 m2 m3 m4 m5 m6 m7 m8 m9 = f <~ m1 ~ m2  ~ m3 ~ m4 ~ m5 ~ m6 ~ m7 ~ m8 ~ m9



Groetjes,

Arian

Arian van Putten

unread,
Feb 24, 2015, 8:56:41 AM2/24/15
to elm-d...@googlegroups.com
Furthermore. I've been working on a rather large piece of elm code..

It interfaces with an API of our company that retreives calendar events + asssociated posters  and displays those posters + calendar on a TV.      I'll ask if I can opensource it as it's a pretty solid example of using JSON + HTTP APIs in Elm.
--
Groetjes,

Arian

Alexey Zlobin

unread,
Feb 26, 2015, 11:21:23 AM2/26/15
to elm-d...@googlegroups.com
Using reference manual as an introduction is extremely bad idea. Reference is a list of whatever the library has in alphabetical or even random order. Introduction tutorial should order things from simple to complex and from basic to custom, providing necessary context and interlinking concepts.

Turning learning into reverse-engineering isn't the way to attract people.

--

Alexander Noriega

unread,
Feb 26, 2015, 7:25:18 PM2/26/15
to elm-d...@googlegroups.com
> Using reference manual as an introduction is extremely bad idea. Reference is a list of whatever the library has in alphabetical or even random order

The Json library's docs are in neither order. They are ordered logically from what you want to do, to what you need in order to do it.

> Turning learning into reverse-engineering isn't the way to attract people

Good thing I'm not doing that at all, then. 

Type signatures, examples, tutorials. All of them good and complementary things.
Reply all
Reply to author
Forward
0 new messages