问题
I've a .json file that look like :
[
{
"network": "X.X.X.1",
"defaultGateway": "X.X.X.X",
"ipAddressTab": [
{
"foo1": "10.0.0.1",
"foo2": "network",
"foo3": "reserved",
"foo4": null,
"foo5": null,
"foo6": null,
"foo7": null,
"foo8": null,
"foo9": null,
"foo10": null,
"foo11": null
},
{
"foo1": "10.0.0.2",
"foo2": "network",
"foo3": "reserved",
"foo4": null,
"foo5": null,
"foo6": null,
"foo7": null,
"foo8": null,
"foo9": null,
"foo10": null,
"foo11": null
},
{
"foo1": "10.0.0.3",
"foo2": "network",
"foo3": "reserved",
"foo4": null,
"foo5": null,
"foo6": null,
"foo7": null,
"foo8": null,
"foo9": null,
"foo10": null,
"foo11": null
},
{
"foo1": "10.0.0.4",
"foo3": "available"
},
{
"foo1": "10.0.0.5",
"foo3": "available"
},
{
"foo1": "10.0.0.6",
"foo3": "available"
},
{
"foo1": "10.0.0.7",
"foo3": "available"
}
],
"full": false,
"id": "xxx"
},
{
"network": "X.X.X.2",
"defaultGateway": "X.X.X.X",
"ipAddressTab": [
{
"foo1": "10.0.0.1",
"foo2": "network",
"foo3": "reserved",
"foo4": null,
"foo5": null,
"foo6": null,
"foo7": null,
"foo8": null,
"foo9": null,
"foo10": null,
"foo11": null
},
{
"foo1": "10.0.0.2",
"foo2": "network",
"foo3": "reserved",
"foo4": null,
"foo5": null,
"foo6": null,
"foo7": null,
"foo8": null,
"foo9": null,
"foo10": null,
"foo11": null
},
{
"foo1": "10.0.0.3",
"foo2": "network",
"foo3": "reserved",
"foo4": null,
"foo5": null,
"foo6": null,
"foo7": null,
"foo8": null,
"foo9": null,
"foo10": null,
"foo11": null
},
{
"foo1": "10.0.0.4",
"foo3": "available"
},
{
"foo1": "10.0.0.5",
"foo3": "available"
},
{
"foo1": "10.0.0.6",
"foo3": "available"
},
{
"foo1": "10.0.0.7",
"foo3": "available"
}
],
"full": false,
"id": "xxx"
}
]
The first thing that I try that is to display each element like that :
cat myfile.txt | jq '.[]| {network: .network, ipAddressTab: .ipAddressTab}'
Each lines of the file xas displayed.
Now, I want to display some lines :
- network
- ipAddressTab
- foo1
- foo3
{
"network": "X.X.X.1",
"ipAddressTab": [
{
"foo1": "10.0.0.1",
"foo3": "reserved",
"foo1": "10.0.0.2",
"foo3": "reserved",
"foo1": "10.0.0.3",
"foo3": "reserved",
"foo1": "10.0.0.4",
"foo3": "available"
"foo1": "10.0.0.5",
"foo3": "available"
"foo1": "10.0.0.6",
"foo3": "available"
"foo1": "10.0.0.7",
"foo3": "available"
}
],
"full": false,
},
{
"network": "X.X.X.2",
"ipAddressTab": [
{
"foo1": "10.0.0.1",
"foo3": "reserved",
"foo1": "10.0.0.2",
"foo3": "reserved",
"foo1": "10.0.0.3",
"foo3": "reserved",
"foo1": "10.0.0.4",
"foo3": "available"
"foo1": "10.0.0.5",
"foo3": "available"
"foo1": "10.0.0.6",
"foo3": "available"
"foo1": "10.0.0.7",
"foo3": "available"
}
],
"full": false,
}
]
I've try something like :
cat myfile.txt | jq '.[]| {network: .network, ipAddressTab: .ipAddressTab(.foo1, .foo3)}'
cat myfile.txt | jq '.[]| {network: .network, ipAddressTab: .ipAddressTab[.foo1, .foo3]}'
But it doesn't work !
Some one to show me how to display each elements that I need ?
Thansk a lot !
回答1:
First select the fields, then modify ipAddressTab
.
map({network, ipAddressTab, full}
| .ipAddressTab[] |= {foo1, foo3})
Online demo
来源:https://stackoverflow.com/questions/65277686/select-multiple-fields-at-different-levels