2016-04-14 54 views
0

我有這樣的結構的JSON:如何使用jq搜索json的值?

{ 
    "nodes": { 
    "60e327ee58a0": { 
     "nodeinfo": { 
     "network": { 
      "mesh": { 
      "bat0": { 
       "interfaces": { 
       "wireless": [ 
        "<mac-address-removed>" 
       ], 
       "tunnel": [ 
        "<mac-address-removed>" 
       ] 
       } 
      } 
      }, 
      "mac": "<mac removed>", 
      "addresses": [ 
      "<ipv6 removed>", 
      "<ipv6 removed>" 
      ] 
     }, 
     "hardware": { 
      "model": "TP-Link TL-WR841N/ND v10", 
      "nproc": 1 
     }, 
     "software": { 
      "batman-adv": { 
      "compat": 15, 
      "version": "2015.1" 
      }, 
      "autoupdater": { 
      "branch": "stable", 
      "enabled": true 
      }, 
      "firmware": { 
      "release": "v2016.1+1.0.1", 
      "base": "gluon-v2016.1" 
      }, 
      "status-page": { 
      "api": 1 
      }, 
      "fastd": { 
      "enabled": true, 
      "version": "v17" 
      } 
     }, 
     "hostname": "Antoniusweg12", 
     "system": { 
      "site_code": "ffmsd03" 
     }, 
     "node_id": "60e327ee58a0" 
     }, 
     "lastseen": "2016-04-14T12:39:04", 
     "flags": { 
     "gateway": false, 
     "online": true 
     }, 
     "firstseen": "2016-03-16T15:14:04", 
     "statistics": { 
     "clients": 1, 
     "gateway": "de:ad:be:ef:43:02", 
     "rootfs_usage": 0.6041666666666667, 
     "loadavg": 0.09, 
     "uptime": 1822037.41, 
     "memory_usage": 0.8124737210932025, 
     "traffic": { 
      "rx": { 
      "packets": 50393821, 
      "bytes": 5061895206 
      }, 
      "forward": { 
      "packets": 173, 
      "bytes": 17417 
      }, 
      "mgmt_rx": { 
      "packets": 47453745, 
      "bytes": 6623785282 
      }, 
      "tx": { 
      "packets": 1205695, 
      "bytes": 173509528, 
      "dropped": 5683 
      }, 
      "mgmt_tx": { 
      "packets": 37906725, 
      "bytes": 11475209742 
      } 
     } 
     } 
    }, 
    "30b5c2b042f4": { 
<next block...> 

而且我想用JQ的主機名,MAC或IPv6的進行查詢。作爲對象有一個索引

感謝您的幫助提前

cat nodes.json |jq -c '.nodes[] | select(.nodes[]| contains("Antoniusweg12"))' 

大多數例子不適合這種JSON結構。

回答

0

如果要過濾,則需要深入查看要檢查的屬性並查看它是否符合條件。你不能指望給出一個名字,你會神奇地被提出你想要的結果。

由主機名搜索,它是在每個節點的.nodeinfo.hostname屬性中找到:

$ jq -c --arg hostname "Antoniusweg12" \ 
'.nodes[] | select(.nodeinfo.hostname == $hostname)' nodes.json 

同樣的MAC地址,它是在.nodeinfo.network.mac屬性中找到:

$ jq -c --arg mac "aa:bb:cc:dd:ee:ff" \ 
'.nodes[] | select(.nodeinfo.network.mac == $mac)' nodes.json 

對於IP地址,它們有一個數組,但在查詢中沒有太大差別。他們被發現在.nodeinfo.network.addresses屬性:

$ jq -c --arg ip "aaaa:bbbb:cccc:dddd::1" \ 
'.nodes[] | select(.nodeinfo.network.addresses[] == $ip)' nodes.json 
0

這是另一個問題。假設您想要查找鍵值「Antoniusweg12」,密鑰「主機名稱」, 的所有匹配項,無論鍵/值組合出現在何處。

下面將揭示的路徑,感興趣的鍵/值組合:

paths as $p 
| select ($p[-1] == "hostname" and getpath($p) == "Antoniusweg12") 
| $p 

給定的輸入JSON結果:

[ 
    "nodes", 
    "60e327ee58a0", 
    "nodeinfo", 
    "hostname" 
] 

如果你想要的路徑包含對象,然後用$p[0:-1]替換最後的$p;如果你想包含對象本身:getpath($p[0:-1])

0

這裏是節點中的溶液,其搜索其中指定的$needle是存在於任何addressesmachostname字段。

"<ipv6 removed>" as $needle # set to whatever you like 

| foreach (.nodes|keys[]) as $k (
    . 
    ; . 
    ; (  .nodes[$k].nodeinfo.network.addresses? 
     + [ .nodes[$k].nodeinfo.network.mac? 
      , .nodes[$k].nodeinfo.hostname? 
      ] 
     ) as $haystack 

    | if $haystack | index($needle) 
     then {($k): .nodes[$k]} 
     else empty 
     end 
) 

編輯:我現在意識到形式foreach E as $X (.; .; R)幾乎總是可以改寫爲E as $X | R的過濾器,上面是真的只是

"<ipv6 removed>" as $needle 

| (.nodes|keys[]) as $k 
| (  .nodes[$k].nodeinfo.network.addresses? 
    + [ .nodes[$k].nodeinfo.network.mac? 
     , .nodes[$k].nodeinfo.hostname? 
    ] 
) as $haystack 

| if $haystack | index($needle) 
    then {($k): .nodes[$k]} 
    else empty 
    end