json 在jq查询中使用Select进行筛选

0mkxixxg  于 2022-11-19  发布在  其他
关注(0)|答案(2)|浏览(135)

以前我在这里发布了一个问题(从json文件创建一个列表,有多个值)。2然而,我发现我的一些键已经过时了,它们包含一个叫做“end”的键。3我无法过滤掉它们,使得数据不稳定。
下面是我用于测试的更新后的JSON:

[
  {
    "primary": "JOHN DOE",
    "attributes": [
      {
        "type": "double",
        "name": "BUILDING_NUMBER",
        "value": "123"
      },
      {
        "type": "double",
        "name": "FLOOR",
        "value": 10
      },
      {
        "type": "string",
        "name": "EMAIL",
        "value": "john.doe@contoso.com"
      },
      {
        "type": "string",
        "name": "JOB_TITLE",
        "value": "SALESMAN"
      },
      {
        "type": "string",
        "name": "JOB_TITLE",
        "value": "TRAINEE",
        "end": "11-09-2022"
      }
    ],
    "aliases": [
      {
        "alias": "joao.doe@contoso.com"
      }
    ]
  },
  {
    "primary": "LORRAINE DOE",
    "attributes": [
      {
        "type": "double",
        "name": "BUILDING_NUMBER",
        "value": 456
      },
      {
        "type": "double",
        "name": "FLOOR",
        "value": 10
      },
      {
        "type": "string",
        "name": "STATUS",
        "value": "Unavaliable"
      },
      {
        "type": "string",
        "name": "EMAIL",
        "value": "lorraine.doe@contoso.com"
      },
      {
        "type": "string",
        "name": "JOB_TITLE",
        "value": "Procurement",
        "end": "11-09-2021"
      },
      {
        "type": "string",
        "name": "JOB_TITLE",
        "value": "SECRETARY"
      },
      {
        "type": "string",
        "name": "JOB_TITLE",
        "value": "SALES",
        "end": "11-09-2021"
      }
    ],
    "aliases": [
      {
        "alias": "lorris.doe@contoso.com"
      },
      {
        "alias": "lorris2.doe@contoso.com"
      }
    ]
  },
  {
    "primary": "JACK DOE",
    "attributes": [
      {
        "type": "double",
        "name": "BUILDING_NUMBER",
        "value": "123"
      },
      {
        "type": "double",
        "name": "FLOOR",
        "value": 10
      },
      {
        "type": "string",
        "name": "JOB_TITLE",
        "value": "OWNER"
      }
    ],
    "aliases": [
      {
        "alias": "jack.doe@contoso.com"
      },
      {
        "alias": "jackson.doe@contoso.com"
      }
    ]
  },
  {
    "primary": "NOAH DOE",
    "attributes": [
      {
        "type": "double",
        "name": "BUILDING_NUMBER",
        "value": "123"
      },
      {
        "type": "double",
        "name": "FLOOR",
        "value": 10
      },
      {
        "type": "string",
        "name": "EMAIL",
        "value": "noah.doe@contoso.com"
      }
    ],
    "aliases": [
      {
        "alias": "noah.doe95@contoso.com"
      }
    ]
  }
]

是否有办法过滤掉关键字为“end”的属性,并使结果如下所示?

"john.doe@contoso.com": "SALESMAN",
"lorraine.doe@contoso.com": "SECRETARY"

这是老办法:

.[].attributes | from_entries
| select(has("JOB_TITLE") and has("EMAIL"))
| "\"\(.EMAIL)\": \"\(.JOB_TITLE)\""
fdbelqdn

fdbelqdn1#

在运行from_entries之前,使用map将它们过滤掉:
第一个
Demo

tjrkku2a

tjrkku2a2#

在将属性转换为对象之前,请仅选择那些不具有end特性的对象:

.[].attributes
| map(select(has("end")|not)) # <- this line is new
| from_entries
| select(has("JOB_TITLE") and has("EMAIL"))
| "\"\(.EMAIL)\": \"\(.JOB_TITLE)\""

输出量:

"john.doe@contoso.com": "SALESMAN"
"lorraine.doe@contoso.com": "SECRETARY"

相关问题