简体   繁体   中英

How to extract the specific field value from the json using jq by passing the input key

I have the below JSON and would like to get the ip value if the name value matches with the passed input value.

ie pass the input as abc.com and return the value as 101.0.0.0. How can I achieve this using jq?

[  
  {  
     "ips":[  
        {  
           "name":"google.com",
           "value":"172.217.164.110"
        }
     ]
   },
   {
     "ips":[  
        {  
           "name":"abc.com",
           "value":"101.0.0.0"
        }
      ]
    }
]

I want to retrieve the value of the field only if the value passed from outside is matched with the other field in the same array.

ie When I pass key as abc.com which should return the value 101.0.0.0

With your input, the invocation:

jq -r --arg name abc.com '.[][][] | select(.name == $name).value'

produces:

101.0.0.0

You might also like to consider alternatives such as:

jq -r --arg name abc.com '.. | objects | select(.ips) | .ips[] | select(.name == $name).value'

or, throwing caution to the wind:

jq -r --arg name abc.com '.. | objects | select(.name == $name).value'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM