2017-10-17 4 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

は、指定された出力を生成するを使用するフィルタである:

map(map_values(._metadata.timestamp)) 

サンプルラン(data.jsonにデータを想定)

$ 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

関連する問題