Select multiple fields at different levels

[亡魂溺海] 提交于 2021-02-16 20:51:30

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!