Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a 'keys' function, or maybe 'path' #5

Open
jfischoff opened this issue Jan 29, 2014 · 4 comments
Open

Add a 'keys' function, or maybe 'path' #5

jfischoff opened this issue Jan 29, 2014 · 4 comments

Comments

@jfischoff
Copy link

Instead of writing

livingOnTheEdge = foo ^?! key "this" . key "is" . key "annoying"

I can write

keys = foldl' (\x f -> f . key x) id

livingOnTheEdge = foo ^?! keys ["this", "is","less", "annoying"]

Seems common enough to have in the library

@bitemyapp
Copy link

This project is deprecated and has been moved into Lens 4.

@jfischoff if this feature isn't in the Lens 4 version, could you refile this issue there?

@glguy
Copy link
Member

glguy commented Aug 18, 2014

Project is back in this repository, let's reopen the discussion about this being a good idea or not and then implement it or close the issue :)

@rampion
Copy link

rampion commented May 31, 2017

If you want to be less verbose, why not

data X (f :: * -> *) = X {getX :: forall t. AsValue t => (Value -> f Value) -> t -> f t}

keys :: (AsValue t, Applicative f) => T.Text -> (Value -> f Value) -> t -> f t
keys = getX . L.foldr1 (\(X a) (X b) -> X (a . b)) . map (\k -> X $ key k) . T.splitOn "."

So now you do

λ "{ \"a\": { \"b\": { \"c\": 3 } } }" ^? keys "a.b.c"
Just (Number 3.0)

gasi added a commit to zoomhub/zoomhub that referenced this issue Aug 31, 2018
And, `lens-aeson` is back, according to this:
lens/lens-aeson#5 (comment)

This reverts commit becdc8b.
@sjshuck
Copy link

sjshuck commented Mar 7, 2022

I think if key "foo" . key "bar" . key "baz" were ever deemed insufficient, the next step up should be something more substantial than to splitOn "." Perhaps incorporating https://hackage.haskell.org/package/jsonpath. lens-aeson could have a function doing [JSONPathElement] -> Traversal' Value Value, maybe a cool quasi-quoter validating syntax during compilation.

Although it may be more suitable as its own library, or incorporated into jsonpath using traversals-in-disguise or microlens.

EDIT: This exists: https://hackage.haskell.org/package/aeson-picker

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants