So the thing is that I have a JSON looking a little bit something like this:
{
"country": "USA",
"states": [
{
"stateName": "California",
"timeZone": "UTC-8",
"cities": [
{
"cityName": "Los Angeles"
},
{
"cityName": "San Diego"
}
]
},
{
"stateName": "Texas",
"timeZone": "UTC-8",
"cities": [
{
"cityName": "Houston"
},
{
"cityName": "Dallas"
}
]
}
]
}
(that json is just an example, don't take it seriously)
What I need to do is to list all the cities but also to grab the "stateName" field from the parent and somehow put it inside the city object resulting in something like this:
[
{
"stateName": "California",
"cityName": "Los Angeles"
},
{
"stateName": "Texas",
"cityName": "Houston"
},
{
"stateName": "Texas",
"cityName": "Dallas"
},
{
"stateName": "California",
"cityName": "San Diego"
}
]
I tried using the built-in merge function in a few different ways but I didn't manage to get any results, besides the documentation on that function is not very intuitive.
one of my failed attempts:
merge(`states[].cities[]`, `states[].{stateName: stateName}`)
Any ideas?
Thanks in advance.
You can't do this using jmespath. Check this out : https://github.com/jmespath/jmespath.js/issues/22#issuecomment-394157797
However, you can do something like this for now:
states[].{stateName: stateName, cities: cities[].cityName}[]
to result into an output like:
[
{
"stateName": "California",
"cities": [
"Los Angeles",
"San Diego"
]
},
{
"stateName": "Texas",
"cities": [
"Houston",
"Dallas"
]
}
]