当json具有多个相同条目时,如何让jq返回唯一结果? [英] How do I get jq to return unique results when json has multiple identical entries?

查看:110
本文介绍了当json具有多个相同条目时,如何让jq返回唯一结果?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

jq '
  .[]|select(.accountEnabled==true)|select(.assignedPlans[].service=="exchange" and .assignedPlans[].capabilityStatus=="Enabled").proxyAddresses[]'

下面是json的示例,它是匿名的"az广告用户列表"(从Azure中获取Active Directory用户列表)的输出,并删除了不相关的内容.上面是我要用来提取电子邮件地址的jq命令,所需的输出是"SMTP:russell.coker@example.com",打印一次而不是9次.是的,我知道我可以将其传递给Unix命令"sort -u",但我想对其进行其他json查询.

Below is a sample of json, it's the output of "az ad user list" (getting the Active Directory userlist from Azure) anonymised and with irrelevant things removed. Above is a jq command that I want to use to extract email addresses, the desired output is "SMTP:russell.coker@example.com" printed once not 9 times. Yes, I know I could pipe this to the Unix command "sort -u" but I'd like to do other json queries on it.

[
  {
    "accountEnabled": true,
    "assignedPlans": [
      {
        "capabilityStatus": "Enabled",
        "service": "exchange"
      },
      {
        "capabilityStatus": "Enabled",
        "service": "exchange"
      },
      {
        "capabilityStatus": "Enabled",
        "service": "exchange"
      }
    ],
    "provisionedPlans": [
      {
        "capabilityStatus": "Enabled",
        "provisioningStatus": "Success",
        "service": "exchange"
      },
      {
        "capabilityStatus": "Enabled",
        "provisioningStatus": "Success",
        "service": "exchange"
      },
      {
        "capabilityStatus": "Enabled",
        "provisioningStatus": "Success",
        "service": "exchange"
      },
      {
        "capabilityStatus": "Enabled",
        "provisioningStatus": "Success",
        "service": "exchange"
      }
    ],
    "proxyAddresses": [
      "SMTP:russell.coker@example.com"
    ]
  },
  {
    "accountEnabled": true,
    "assignedPlans": [
      {
        "capabilityStatus": "Deleted",
        "service": "exchange"
      },
      {
        "capabilityStatus": "Deleted",
        "service": "OfficeForms"
      }
    ],
    "provisionedPlans": [
      {
        "capabilityStatus": "Deleted",
        "provisioningStatus": "Success",
        "service": "SharePoint"
      },
      {
        "capabilityStatus": "Deleted",
        "provisioningStatus": "Success",
        "service": "exchange"
      },
      {
        "capabilityStatus": "Deleted",
        "provisioningStatus": "Success",
        "service": "exchange"
      }
    ],
    "proxyAddresses": [
      "smtp:a@example.com",
      "smtp:b@example.com",
      "SMTP:c@example.com"
    ]
  }
]

推荐答案

也许问题在于给定的jq查询完全是错误的",因为它无法捕获OP的意图.

Perhaps the problem is that the given jq query is simply "wrong" in that it does not capture the OP's intent.

即使以下查询不能反映OP的意图,值得注意的是,使用给定的JSON,它会产生所需的单个结果:

Even if the following query does not reflect the OP's intent, it is worth noting that, with the given JSON, it produces the single result that is wanted:

.[]
| select(.accountEnabled==true)
| select(any(.assignedPlans[];
             .service=="exchange" and
             .capabilityStatus=="Enabled"))
| .proxyAddresses[]

类似....

这是另一个具有不同语义的查询,但是使用给定的JSON也会生成单个所需的结果. (这表明,单个示例本身不能替代需求.)

Likewise ....

Here's another query with different semantics but which, with the given JSON, also produces the single desired result. (It goes to show that a single example by itself is no substitute for requirements.)

.[]
 | select(.accountEnabled==true)
 | select(any(.assignedPlans[]; .service=="exchange"))
 | select(any(.assignedPlans[]; .capabilityStatus=="Enabled"))
 | .proxyAddresses[]

这篇关于当json具有多个相同条目时,如何让jq返回唯一结果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆