使用jq,我如何根据键限制值

时间:2015-09-20 02:43:09

标签: jq

对于如下所示的输入文件:

{
  "employees": [
    {
      "number": "101",
      "tags": [
        {
          "value": "yes",
          "key": "management"
        },
        {
          "value": "joe",
          "key": "login"
        },
        {
          "value": "joe blogs",
          "key": "name"
        }
      ]
    },
    {
      "number": "102",
      "tags": [
        {
          "value": "no",
          "key": "management"
        },
        {
          "value": "jane",
          "key": "login"
        },
        {
          "value": "jane doe",
          "key": "name"
        }
      ]
    },
    {
      "number": "103",
      "tags": [
        {
          "value": "no",
          "key": "management"
        },
        {
          "value": "john",
          "key": "login"
        },
        {
          "value": "john doe",
          "key": "name"
        }
      ]
    }
  ]
}

...我想获得所有非管理层员工的详细信息,以便所需的输出如下:

{
  "number": "102",
  "name": "jane doe",
  "login": "jane"
}
{
  "number": "103",
  "name": "john doe",
  "login": "john"
}

我无法弄清楚如何在不选择密钥的情况下根据密钥限制结果(在本例中为“管理”)

2 个答案:

答案 0 :(得分:2)

以下是一个稍微简洁的解决方案:

.employees[]
| .tags |= from_entries
| select(.tags.management == "no")
| {number, "name": .tags.name, "login": .tags.login}

答案 1 :(得分:0)

这是另一个使用from_entries

的解决方案
  .employees[]
| {number} + (.tags | from_entries)
| if .management == "no" then {number, name, login} else empty end