#json #jq #projection
#json #jq #проекция
Вопрос:
У меня есть файл .json, который выглядит как :
[
{
"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"
}
]
Первое, что я пытаюсь сделать, это отобразить каждый элемент таким образом :
cat myfile.txt | jq '.[]| {network: .network, ipAddressTab: .ipAddressTab}'
Отображаются все строки файла xas.
Теперь я хочу отобразить несколько строк :
- сеть
- 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,
}
]
Я попробовал что-то вроде :
cat myfile.txt | jq '.[]| {network: .network, ipAddressTab: .ipAddressTab(.foo1, .foo3)}'
cat myfile.txt | jq '.[]| {network: .network, ipAddressTab: .ipAddressTab[.foo1, .foo3]}'
Но это не работает!
Кто-нибудь покажет мне, как отображать все элементы, которые мне нужны?
Спасибо большое !
Ответ №1:
Сначала выберите поля, затем измените ipAddressTab
.
map({network, ipAddressTab, full}
| .ipAddressTab[] |= {foo1, foo3})