You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

JSONPath查询group_names含Registered的用户name结果为空求助

解决JSONPath筛选包含指定字符串用户的问题

问题根源

你当前的JSONPath有两个关键错误:

  1. 路径遍历错误:$.data是数组类型,不能直接用.attributes访问,需要先遍历数组中的每个元素;
  2. 匹配逻辑错误:使用==是精确匹配,但部分用户的group_names包含多个分组(比如Registered\nSuper Users),需要用包含匹配而非精确匹配。

正确的JSONPath表达式

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

表达式拆解

  • $.data[*]:遍历data数组中的所有用户对象;
  • [?(@.attributes.group_names contains 'Registered')]:筛选出attributes.group_names字符串中包含Registered的用户;
  • .attributes.name:提取符合条件用户的name字段。

补充说明

不同JSONPath实现对字符串包含的语法可能略有差异,如果上述表达式不生效,可以尝试以下替代写法:

  • 基于正则匹配(部分支持正则的JSONPath引擎):
    $.data[?(@.attributes.group_names =~ /Registered/)].attributes.name
    
  • 用indexOf函数(部分引擎支持):
    $.data[?(@.attributes.group_names.indexOf('Registered') != -1)].attributes.name
    

验证结果

用你的API响应数据测试,正确结果应该是:

["Bryan", "easy", "Machin"]

内容的提问来源于stack exchange,提问作者Olivier Sohy

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.12 14:00:00