[Haskell-cafe] JSON querying
Sergey Mironov
ierton at gmail.com
Mon Feb 18 08:20:43 CET 2013
2013/2/17 Oliver Charles <ollie at ocharles.org.uk>:
> On 02/17/2013 03:01 PM, Sergey Mironov wrote:
>>
>> Hi folks. Hackage contains several JSON packages but as far as I see,
>> they all provide 'static' conversion from JSON format to Haskell data
>> type. Is there a method of converting object containing optional filed
>> 'a' to for example Maybe a.
>
> Assuming you have some sort of 'path' to the key in question, aeson-lens
> might be exactly what you want:
> http://hackage.haskell.org/package/aeson-lens
>
> I use aeson-lens to turn a list of strings of the form ["foo", "bar", "baz"]
> into a query into first the 'foo' object, then the 'bar' object, then the
> 'baz' object, using the following:
>
> pathToLens :: Functor f
> => [T.Text]
> -> (Maybe Value -> f (Maybe Value))
> -> Maybe Value
> -> f (Maybe Value)
> pathToLens [p] = key p
> pathToLens ps = let ps' = filter (not . T.null) ps
> in key (head ps') . (foldl (.) id . map pathElem $ tail ps')
> where
> pathElem p = maybe (key p) nth (readMay $ T.unpack p)
>
> So as you can see - I turn any arbitrary path into a query into JSON, with a
> chance of failure.
>
> Hope this helps!
> - Ollie
>
>
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe
Good news, thanks a lot!
Sergey
More information about the Haskell-Cafe
mailing list