| Safe Haskell | None |
|---|---|
| Language | Haskell2010 |
Data.Aeson.Transform
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 } => 2Dig deeper
At "foo" $ Attr "bar"
-- { foo: { bar: 3 }} => 3Map 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:3, c:4 }Transformation builder
Transformations are specified by creating Builder instances.
Builders specify how to navigate through input JSON and construct
output at various nodes in the tree.
Constructors
| Id | Pass input directly as output |
| At Text Builder | Move to value in current object |
| Attr Text | Get value in current object |
| Keep [Text] | Filter current object by keys |
| Map Builder | Map over input array |
| Index Int | Get value at index of current array |
| AtIndex Int Builder | Move to index in current array |
| Obj (HashMap Text Builder) | Produce object with given keys |
| Merge Builder Builder | Combine two objects |