我有一个
的JSON示例{
"reason": "TCP FINs",
"bytes": "1759",
"duration": "0:00:00",
"dest": {
"interface": "inside",
"ip": "192.168.2.2",
"port": "88"
},
"source": {
"interface": "outside",
"ip": "10.1.5.1",
"port": "60470"
},
"connection-id": "400508768",
"proto": "TCP",
"hostname": "192.168.1.1",
"timestamp": "Apr 28 13:00:01",
"event.tags": [ "cisco", "ASA-6-302014" ]
}
我希望提取出源和目标部分,但是作为一个对象。
{
"dest": {
"interface": "inside",
"ip": "192.168.2.2",
"port": "88"
},
"source": {
"interface": "outside",
"ip": "10.1.5.1",
"port": "60470"
}
}
如果我做jq' .source,.dest"然后我得到两个不同的对象
"dest": {
"interface": "inside",
"ip": "192.168.2.2",
"port": "88"
}
"source": {
"interface": "outside",
"ip": "10.1.5.1",
"port": "60470"
}
如果使用-c,它会更明显,因为它们是两条不同的行
作为一个更高级的阶段,我希望能够在没有其他数据的情况下获得其中的一部分(IP地址),但仍然作为对象
{
"dest": {
"ip": "192.168.2.2",
},
"source": {
"ip": "10.1.5.1",
}
}
答案 0 :(得分:3)
试试这个:
jq '{ "dest": .dest, "source": .source }'
或您的最终表格:
jq '{ "dest": { "ip": .dest.ip }, "source": { "ip": .source.ip } }'
输出:
{
"dest": {
"ip": "192.168.2.2"
},
"source": {
"ip": "10.1.5.1"
}
}