我正在尝试使用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"
]
哪种过滤器可以实现这一目标?
答案 0 :(得分:1)
这是一个使用map_values生成指定输出的过滤器:
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"
}
]