This is an alternative site for discovering Elm packages. You may be looking for the official Elm package site instead.
2.0.0
Yet another JSON library inspired by Yojson
version 1.0.0
license BSD3
native-modules False
elm-version 0.18.0 <= v < 0.19.0
Tag 1.0.0
Committed At 2018-05-03 11:34:13 UTC
elm-lang/core 5.1.1 <= v < 6.0.0 5.1.1

README

Yajson Build Status

I tried to port some of the Yojson functionalities - in particular the Yojson.Basic.Util module - in Elm.

This library provides dynamic json access and manipulation without the need to write decoders.

Why

Elm has a great json library and ecosystem, but in certain situations, in an elm-repl session for example, I'd like to have a more flexible interface. This library, anyway, was written to learn and explore Elm and the use case is more scholastic than practical. It's not meant to replace any of the standard library.

Examples

Say we have a json like the following:

rawJson : String
rawJson =
    """
    { "squadName": "Super hero squad"
    , "homeTown": "Metro City"
    , "formed": 2016
    , "secretBase": "Super tower"
    , "active": true
    , "members":
        [
            { "name": "Molecule Man"
            , "age": 29
            , "secretIdentity": "Dan Jukes"
            , "powers":
                [ "Radiation resistance"
                , "Turning tiny"
                , "Radiation blast"
                ]
            }
        ,
            { "name": "Madame Uppercut"
            , "age": 39
            , "secretIdentity": "Jane Wilson"
            , "powers":
                [ "Million tonne punch"
                , "Damage resistance"
                , "Superhuman reflexes"
                ]
            }
        ,
            { "name": "Eternal Flame"
            , "age": 1000000
            , "secretIdentity": "Unknown"
            , "powers":
                [ "Immortality"
                , "Heat Immunity"
                , "Inferno"
                , "Teleportation"
                , "Interdimensional travel"
                ]
            }
        ]
    }
    """

if we want to a list of powers, with standard library the code may look like this

powersDecoder : Json.Decode.Decoder (List (List String))
powersDecoder =
    Json.Decode.string
        |> Json.Decode.list
        |> Json.Decode.field "powers"
        |> Json.Decode.list
        |> Json.Decode.field "members"


powers : List String
powers =
    rawJson
        |> Json.Decode.decodeString powersDecoder
        |> Result.withDefault []
        |> List.concat

while with Yajson we could write

powers_ : List String
powers_ =
    rawJson
        |> Yajson.fromString
        |> Result.withDefault Yajson.Null
        |> Yajson.member "members"
        |> Maybe.map Yajson.toList
        |> Maybe.withDefault []
        |> Yajson.filterMember "powers"
        |> Yajson.flatten
        |> Yajson.filterString

For more usage examples look here.