2017-02-24 25 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

過得好是被添加到每個對象osm_id? –

回答

0

我昨天剛剛回答了一個類似的問題。

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

注意,所需輸出是一個數組[]當它應該是一個對象(該鍵)。

(上一個問題是https://stackoverflow.com/a/42428341/7613900

+0

完美!非常感謝你。 – drumttocs8