2017-02-24 8 views
-3

特定の配列の各オブジェクトにidを割り当てることでJSONを再構築しようとしています。このIDは、既存のキー値(osm_id)を使用して作成されます。jqを使用してJSONキー値からオブジェクトIDを作成

これは私の入力です:

[ 
{ 
    "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" 
} 
] 

これは私の所望の出力です:

[ 
"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" 
} 
] 

私はデータを更新するJQで動作するようにしようとしてきたが、私は把握することはできませんその最上位IDをどのように割り当てるか。これまでのところ私は持っています

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

しかし、それ以上のエラーが発生します。

何か助けていただきありがとうございます。

+0

? –

答えて

0

私は昨日同様の質問をしました。

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

希望の出力は、それが(キーの)オブジェクトであるべきである配列[]であることに留意されたいです。

(前の質問だったhttps://stackoverflow.com/a/42428341/7613900)どのようにして、各オブジェクトに追加されますosm_idを取得している

+0

パーフェクト!どうもありがとうございました。 – drumttocs8

関連する問題