I have a bunch of nested JSON objects with arbitrary keys.
{
"A": {
"B": {
"C": "hello"
}
}
}
Where A
, B
, C
are unknown ahead of time. Each of those three could also
have siblings.
I'm wondering if there is a way to parse this into a custom type with Aeson in
some elegant way. What I have been doing is loading it into an Aeson Object
.
How would you go about implementing the FromJSON
for this kind of JSON
object?
Thanks!
Edit:
{
"USA": {
"California": {
"San Francisco": "Some text"
}
},
"Canada": {
...
}
}
This should compile to CountryDatabase
where...
type City = Map String String
type Country = Map String City
type CountryDatabase = Map String Country
You can reuse
FromJSON
instance ofMap String v
. Something like the next:The output:
PS: You can do it without
newtype
s, I used them only for clarity.