JSONPath 无法正确创建排序

问题描述 投票:0回答:1

我从 Json Path 开始,但我被卡住了。

我想要一份在“group_names”中输入“注册”的所有人员的“姓名”列表。

我尝试这条路:

$.data.attributes[?(@.group_names=='Registered')].name

但是我的结果是空的。

这是我完整的 API 响应:

{
  "links": {
    "self": "/api/index.php/v1/users"
  },
  "data": [
    {
      "type": "users",
      "id": "977",
      "attributes": {
        "id": 977,
        "name": "Bryan",
        "username": "Bryan",
        "block": 0,
        "sendEmail": 0,
        "registerDate": "2023-08-21 12:16:14",
        "lastvisitDate": "2023-08-21 12:22:15",
        "lastResetTime": null,
        "resetCount": 0,
        "group_count": 1,
        "group_names": "Registered"
      }
    },
{
      "type": "users",
      "id": "971",
      "attributes": {
        "id": 971,
        "name": "easy",
        "username": "easy",
        "block": 0,
        "sendEmail": 0,
        "registerDate": "2023-08-16 11:32:03",
        "lastvisitDate": "2023-08-21 10:48:12",
        "lastResetTime": null,
        "resetCount": 0,
        "group_count": 2,
        "group_names": "Registered\nSuper Users"
      }
    },
    {
      "type": "users",
      "id": "968",
      "attributes": {
        "id": 968,
        "name": "Machin",
        "username": "Machin",
        "block": 0,
        "sendEmail": 0,
        "registerDate": "2023-06-17 18:08:31",
        "lastvisitDate": "2023-08-25 07:46:04",
        "lastResetTime": null,
        "resetCount": 0,
        "group_count": 1,
        "group_names": "Registered"
      }
    },
    ]

我想这是一个新手问题,但如果有人可以帮助我:-)

非常感谢!

我可以列出一堆名字,但不能排序:-/

jsonpath
1个回答
0
投票
$.data[?(@.attributes.group_names == "Registered")].attributes.name

该表达式将根据“group_names”字段等于“Registered”的条件过滤“data”数组,然后提取相应的“name”字段。
将此 JSON 路径表达式应用于 API 响应,名称列表将是:

  • 布莱恩
  • 机器
© www.soinside.com 2019 - 2024. All rights reserved.