2017-10-17 65 views
0

我正在嘗試使用jq來提取頂級屬性名稱以及所附的時間戳。JQ:按照正確的順序打印字典鍵名和封裝的屬性

JSON輸入:

[ 
    { 
    "Something": { 
     "_metadata": { 
     "timestamp": "2016-02-18T12:32:50.276Z" 
     } 
    } 
    }, 
    { 
    "OtherThing": { 
     "_metadata": { 
     "timestamp": "2016-03-18T12:32:50.276Z" 
     } 
    } 
    }, 
    { 
    "ThirdThing": { 
     "_metadata": { 
     "timestamp": "2016-04-18T12:32:50.276Z" 
     } 
    } 
    } 
] 

所需的輸出:

[ 
    { 
    "Something": "2016-02-18T12:32:50.276Z" 
    }, 
    { 
    "OtherThing": "2016-03-18T12:32:50.276Z" 
    }, 
    { 
    "ThirdThing": "2016-04-18T12:32:50.276Z" 
    } 
] 

試圖jq '.[] | keys'這給我的只有頂層字典的名稱。

[ 
    "Something" 
] 
[ 
    "OtherThing" 
] 
[ 
    "ThirdThing" 
] 

哪個過濾器可以實現這個功能?

回答

1

這裏是使用以產生規定的輸出的濾波器:(在data.json假設數據)

map(map_values(._metadata.timestamp)) 

樣品試驗

$ jq -M 'map(map_values(._metadata.timestamp))' data.json 
[ 
    { 
    "Something": "2016-02-18T12:32:50.276Z" 
    }, 
    { 
    "OtherThing": "2016-03-18T12:32:50.276Z" 
    }, 
    { 
    "ThirdThing": "2016-04-18T12:32:50.276Z" 
    } 
] 

Try it online!

+0

感謝,工程。 – user1877106

相關問題