-3

I am trying to restructure my JSON by assigning an id to each object in a given array. This id would be created by using an existing key value (osm_id).

This is my input:

[ 
{
  "geometry" : {
    "coordinates" : [ -0.7118478, 51.0930284 ],
    "type" : "Point"
  },
  "properties" : {
    "osm_id" : "262661",
    "religion" : "christian"
  },
  "type" : "Feature"
}, 
{
  "geometry" : {
    "coordinates" : [ -0.7207513, 51.0897118 ],
    "type" : "Point"
  },
  "properties" : {
    "denomination" : "catholic",
    "osm_id" : "262662",
    "religion" : "christian"
  },
  "type" : "Feature"
}
]

This is my desired output:

[
"262661": {
  "geometry": {
    "coordinates": [
      -0.7118478,
      51.0930284
    ],
    "type": "Point"
  },
  "properties": {
    "osm_id": "262661",
    "religion": "christian"
  },
  "type": "Feature"
},
"262662": {
  "geometry": {
    "coordinates": [
      -0.7207513,
      51.0897118
    ],
    "type": "Point"
  },
  "properties": {
     "denomination": "catholic",
     "osm_id": "262662",
     "religion": "christian"
  },
  "type": "Feature"
}
]

I've been trying to work with jq to update the data, but I can't figure out how to assign that top level id. So far I have

.[] |= {geometry, properties, type}

but anything further results in errors.

I appreciate any help or input.

drumttocs8
  • 21
  • 4

1 Answers1

0

I just had a similar question answered yesterday.

jq 'map( { (.properties.osm_id|tostring): . } ) | add'

Note that your desired output is an array [] when it should be an object (for the keys).

(prev question was https://stackoverflow.com/a/42428341/7613900)

Community
  • 1
  • 1
Mike N
  • 453
  • 1
  • 4
  • 4