aeson-t-0.0.4: Transform JSON

Safe HaskellNone
LanguageHaskell2010

Data.Aeson.Transform

Contents

Synopsis

Example usage

Filter unwanted attributes from an object

keep ["nice", "good"]

-- { bad: 3, good: 1, nice: 500, evil: -3 }
-- => { good: 1, nice: 500 }

Grab value

attr "foo"

-- { foo: 2 } => 2

Dig deeper

at "foo" $ attr "bar"

-- { foo: { bar: 3 }} => 3

Map stuff

map $ attr "foo"

-- [{ foo:1 }, { foo:2 }] => [1, 2]

Extract indices

map $ index 0

-- [[1,2], [3,4]] => [1, 3]

Create object

obj $ fromList [
    ("first", index 0)
  , ("second", index 1)
  ]

-- ["hi", "bye"] => { first:"hi", second:"bye" }

Transform position

at "ranks" $ atIndex 0 $ attr "name"

-- {ranks: [ { name:"winner", score:12 }, { name:"loser", score: 3 ]}
-- => "winner"

Combine objects

merge (attr "foo") (attr "bar")

-- { foo: { a:1, b:2 }, bar: { b:3, c:4 } }
-- => { a:1, b:2, c:4 }

Transformations

type Builder = Value -> Value Source

at :: Text -> Builder -> Builder Source

Move to value in current object

attr :: Text -> Builder Source

Get value in current object

keep :: [Text] -> Builder Source

Filter current object by keys

map :: Builder -> Builder Source

Map over input array

index :: Int -> Builder Source

Get value at index of current array

atIndex :: Int -> Builder -> Builder Source

Move to index in current array

obj :: HashMap Text Builder -> Builder Source

Produce object with given keys

merge :: Builder -> Builder -> Builder Source

Combine two objects