简体   繁体   English

如何用jq获取数组的索引

[英]How get index of array with jq

I want search this string tb1qpvtnfqqs3cp4ly4375km7n5sga8hkdkujkm854 in that structure我想在那个结构中搜索这个字符串tb1qpvtnfqqs3cp4ly4375km7n5sga8hkdkujkm854

{
  "txid": "67bc5194442dc350312a7c0a5fc7ef912c31bf00b23349b4c3afdf177c91fb2f",
  "hash": "8392ded0647e4166eda342cee409c7d0e1e3ffab24de41866d2e6a7bd0a245b3",
  "version": 2,
  "size": 245,
  "vsize": 164,
  "weight": 653,
  "locktime": 1764124,
  "vin": [
    {
      "txid": "69eed058cbd18b3bf133c8341582adcd76a4d837590d3ae8fa0ffee1d597a8c3",
      "vout": 0,
      "scriptSig": {
        "asm": "0014759fc698313da549948940508df6db93a319096e",
        "hex": "160014759fc698313da549948940508df6db93a319096e"
      },
      "txinwitness": [
        "3044022014a8eb758063c52bc970d42013e653f5d3fb3c190b55f7cfa72680280cc5138602202a873b5cad4299b2f52d8cccb4dcfa66fa6ec256d533788f54440d4cdad7dd6501",
        "02ec8ba22da03ed1870fe4b9f9071067a6a1fda6f582c5c858644e44bd401bfc0a"
      ],
      "sequence": 4294967294
    }
  ],
  "vout": [
    {
      "value": 0.37841708,
      "n": 0,
      "scriptPubKey": {
        "asm": "0 686bc8ce41505642c96f3eb99919fff63f4c0f11",
        "hex": "0014686bc8ce41505642c96f3eb99919fff63f4c0f11",
        "reqSigs": 1,
        "type": "witness_v0_keyhash",
        "addresses": [
          "tb1qdp4u3njp2pty9jt086uejx0l7cl5crc3x3phwd"
        ]
      }
    },
    {
      "value": 0.00022000,
      "n": 1,
      "scriptPubKey": {
        "asm": "0 0b173480108e035f92b1f52dbf4e90474f7b36dc",
        "hex": "00140b173480108e035f92b1f52dbf4e90474f7b36dc",
        "reqSigs": 1,
        "type": "witness_v0_keyhash",
        "addresses": [
          "tb1qpvtnfqqs3cp4ly4375km7n5sga8hkdkujkm854"
        ]
      }
    }
  ],
  "hex": "02000000000101c3a897d5e1fe0ffae83a0d5937d8a476cdad821534c833f13b8bd1cb58d0ee690000000017160014759fc698313da549948940508df6db93a319096efeffffff022c6b410200000000160014686bc8ce41505642c96f3eb99919fff63f4c0f11f0550000000000001600140b173480108e035f92b1f52dbf4e90474f7b36dc02473044022014a8eb758063c52bc970d42013e653f5d3fb3c190b55f7cfa72680280cc5138602202a873b5cad4299b2f52d8cccb4dcfa66fa6ec256d533788f54440d4cdad7dd65012102ec8ba22da03ed1870fe4b9f9071067a6a1fda6f582c5c858644e44bd401bfc0a1ceb1a00",
  "blockhash": "000000009acb8b4f06a97beb23b3d9aeb3df71052dabec94465933b564c27f50",
  "confirmations": 2,
  "time": 1591687001,
  "blocktime": 1591687001
}

I'd like to get the index of vout, in this case 1. is it possible with jq?我想获得 vout 的索引,在这种情况下是 1. jq 可以吗?

The following assumes that you want the index in.vout of the first object which has the given string as a leaf value, and that you have in mind using 0 as the index origin.以下假设您想要第一个 object 的索引 in.vout,它具有给定的字符串作为叶值,并且您考虑使用 0 作为索引原点。

A simple and reasonably efficient jq program that finds all such indices is as follows:查找所有此类索引的简单且相当有效的 jq 程序如下:

.vout
| range(0;length) as $i
| if any(.[$i]|..;
         . == "tb1qpvtnfqqs3cp4ly4375km7n5sga8hkdkujkm854") 
  then $i 
  else empty
  end

With the given input, this in fact yields 1, which is in accordance with the problem description, so we seem to be on right track.对于给定的输入,这实际上产生了 1,这与问题描述一致,因此我们似乎走在了正确的轨道上。

To get the first index, you could wrap the above in first(...) , but in that case the result would be the empty stream if there is no occurrence.要获得第一个索引,您可以将上述内容包装在first(...)中,但在这种情况下,如果没有出现,结果将是空的 stream 。 So perhaps you would prefer to wrap the above in first(...) // null因此,也许您更愿意将上述内容包装在first(...) // null

It's not clear what exactly you want.目前尚不清楚您到底想要什么。

I guess you want the n of the element of vout that contains the given address in its addresses list.我猜你想要vout元素的n ,它在其地址列表中包含给定地址。 That can be achieved with这可以通过

jq '.vout[]
    | select(.scriptPubKey.addresses[] == "tb1qpvtnfqqs3cp4ly4375km7n5sga8hkdkujkm854")
    | .n
   ' file.json

You can also use你也可以使用

select((.scriptPubKey.addresses[]
    | contains("tb1qpvtnfqqs3cp4ly4375km7n5sga8hkdkujkm854")))

to search for the address.搜索地址。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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