3 ms·
A json object has the type signature: type Object = HashMap Text Value which is A JSON "object" (key/value map). The keys of the hashmap are Text and the
by platz 9y ago
A json object has the type signature:
type Object = HashMap Text Value
which is A JSON "object" (key/value map).
The keys of the hashmap are Text and the leaves are "Value" which contain more JSON
here are different well-formed json values that both resolve to the same type, A.Object (either you get a parse error String for not-well-formed json, or an A.Object)
>>> A.eitherDecode "{ \"name\": \"Joe\", \"age\": 12 }" :: Either String A.Object
Right (fromList [("age",Number 12.0),("name",String "Joe")])
>>> A.eitherDecode "{ \"name\": \"Joe\", \"age\": {\"foo\": \"12\"} }" :: Either String A.Object
Right (fromList [("age",Object (fromList [("foo",String "12")])),("name",String "Joe")])
Then you can access values in the HashMap by key just like a hashmap in any other language.
Or, as noted by chowells, you can use lens-aeson which can be thought of as a DSL to perform queries in a dynamic fashion:
retrieve value of "foo" in "age"
>>> s = "{ \"name\": \"Joe\", \"age\": {\"foo\": \"12\"} }" :: String
>>> s ^? key "age" . key "foo"
Just (String "12")