Created
November 17, 2018 16:29
-
-
Save Fingerzam/e6e7ccafd0606579673c2c5ba3eee950 to your computer and use it in GitHub Desktop.
Lens is cool
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[ | |
{ | |
"name": "Lol inc", | |
"departments": [ | |
{ | |
"name": "hr", | |
"employees": [ | |
{ | |
"name": "Alice", | |
"position": "manager", | |
"salary": 4800 | |
}, | |
{ | |
"name": "Adam", | |
"position": "worker", | |
"salary": 3200 | |
}, | |
{ | |
"name": "Brenda", | |
"position": "worker", | |
"salary": 3000 | |
} | |
] | |
}, | |
{ | |
"name": "factory", | |
"employees": [ | |
{ | |
"name": "Bob", | |
"position": "manager", | |
"salary": 3500 | |
}, | |
{ | |
"name": "Kara", | |
"position": "worker", | |
"salary": 2620 | |
}, | |
{ | |
"name": "Erik", | |
"position": "worker", | |
"salary": 2500 | |
} | |
] | |
} | |
] | |
} | |
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env stack | |
{- stack | |
script | |
--install-ghc | |
--resolver lts-12.18 | |
--package lens | |
--package aeson | |
--package text | |
--package lens-aeson | |
--package bytestring | |
-} | |
{-# language OverloadedStrings #-} | |
import Control.Lens (over, (^?), filtered) | |
import Data.Aeson.Lens (values, key, _String, _Number) | |
import Data.Text (Text) | |
import qualified Data.ByteString.Lazy | |
import qualified Data.ByteString.Lazy.Char8 | |
main :: IO () | |
main = do | |
input <- Data.ByteString.Lazy.getContents | |
-- give all workers a 5% raise: | |
Data.ByteString.Lazy.putStr | |
$ over (values.key "departments" | |
.values | |
.key "employees" | |
.values | |
.filtered (\employee -> | |
(employee ^? key "position"._String) | |
== | |
Just "worker") | |
.key "salary" | |
._Number) | |
(* 1.05) | |
input | |
putStrLn "" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[ | |
{ | |
"name": "Lol inc", | |
"departments": [ | |
{ | |
"name": "hr", | |
"employees": [ | |
{ | |
"salary": 4800, | |
"name": "Alice", | |
"position": "manager" | |
}, | |
{ | |
"salary": 3360, | |
"name": "Adam", | |
"position": "worker" | |
}, | |
{ | |
"salary": 3150, | |
"name": "Brenda", | |
"position": "worker" | |
} | |
] | |
}, | |
{ | |
"name": "factory", | |
"employees": [ | |
{ | |
"salary": 3500, | |
"name": "Bob", | |
"position": "manager" | |
}, | |
{ | |
"salary": 2751, | |
"name": "Kara", | |
"position": "worker" | |
}, | |
{ | |
"salary": 2625, | |
"name": "Erik", | |
"position": "worker" | |
} | |
] | |
} | |
] | |
} | |
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
cat input.json | ./lens_aeson.hs | jq . > result.json |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment