I'm trying to parse a JSON file with jq but I can't get it right. How do I parse the following JSON to:
- A single nested object and also add additional top level keys ("cities", "uppsala", and "locations" in the example)
- Use the values for the keys
nameas keys for the objects?
(shortened version of source json):
{
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"properties": {
"name": "Uppsala domkyrka"
},
"geometry": {
"coordinates": [
17.6336159,
59.8581466
]
}
},
{
"type": "Feature",
"properties": {
"name": "Uppsala moské"
},
"geometry": {
"coordinates": [
17.6382165,
59.874003
]
}
}
]
}
I'm trying to parse it to this format:
{
"cities": {
"uppsala": {
"locations": {
"Uppsala domkyrka": {
"name": "Uppsala domkyrka",
"coordinates": {
"_latitude": 59.854,
"_longitude": 17.6261
}
},
"Uppsala moské": {
"name": "Uppsala moské",
"coordinates": {
"_latitude": 59.8581,
"_longitude": 17.6336
}
}
}
}
}
}
This is the command I've arrived at so far:
[.features[] | {name: .properties.name, coordinates: {_latitude: .geometry.coordinates[0], _longitude: .geometry.coordinates[1]}}]
To produce this, which is not quite right:
[
{
"name": "Uppsala domkyrka",
"coordinates": {
"_latitude": 17.6336159,
"_longitude": 59.8581466
}
},
{
"name": "Uppsala moské",
"coordinates": {
"_latitude": 17.6382165,
"_longitude": 59.874003
}
}
]
I haven't managed to get the values for name as keys for the objects.
Any ideas? I've been reading SO for hours and pulling my hair out..
ID