Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

[BUG]JSONPath.extract抛异常 #858

Closed
wangyuanqiu opened this issue Oct 21, 2022 · 5 comments
Closed

[BUG]JSONPath.extract抛异常 #858

wangyuanqiu opened this issue Oct 21, 2022 · 5 comments
Labels
bug Something isn't working
Milestone

Comments

@wangyuanqiu
Copy link

问题描述

JSONPath.extract解析报Exception

环境信息

  • OS信息: [e.g.:Mac macOS Monterey]
  • JDK信息: [e.g.:jdk-17.0.2]
  • 版本信息:[e.g.:Fastjson2 2.0.15]

重现步骤

执行以下代码

String tmpJson = "{\"arr\": [{ \"key\": \"value1\" }, { \"key\": \"value2\" }]}";
String tmpPath = "$.arr[key='value1']";
Object tmpObj = JSONPath.extract(tmpJson, tmpPath);
System.out.println(tmpObj);

期待的正确结果

{"key": "value1"}

相关日志输出

以下是path="$.arr[key='value1']"的异常
Exception in thread "main" com.alibaba.fastjson2.JSONException: TODO : k
at com.alibaba.fastjson2.JSONPath$JSONPathParser.parseArrayAccess(JSONPath.java:6338)
at com.alibaba.fastjson2.JSONPath$JSONPathParser.parse(JSONPath.java:6160)
at com.alibaba.fastjson2.JSONPath.of(JSONPath.java:398)
at com.alibaba.fastjson.JSONPath.extract(JSONPath.java:30)

如果path="$.arr[?(key='value1')]"则输出以下异常
Exception in thread "main" com.alibaba.fastjson2.JSONException: jsonpath syntax error, offset 9, character k, line 1, column 10, fastjson-version 2.0.15 $.arr[?(key='value1')]
at com.alibaba.fastjson2.JSONPath$JSONPathParser.parseFilter(JSONPath.java:6839)
at com.alibaba.fastjson2.JSONPath$JSONPathParser.parseArrayAccess(JSONPath.java:6313)
at com.alibaba.fastjson2.JSONPath$JSONPathParser.parse(JSONPath.java:6160)
at com.alibaba.fastjson2.JSONPath.of(JSONPath.java:398)
at com.alibaba.fastjson.JSONPath.extract(JSONPath.java:30)

附加信息

测试过fastjson 1.2.69版本无此问题

@wangyuanqiu wangyuanqiu added the bug Something isn't working label Oct 21, 2022
@wangyuanqiu wangyuanqiu changed the title [BUG] [BUG]JSONPath.extract抛异常 Oct 21, 2022
@wenshao
Copy link
Member

wenshao commented Oct 21, 2022

String tmpJson = "{\"arr\": [{ \"key\": \"value1\" }, { \"key\": \"value2\" }]}";
String tmpPath = "$.arr[?(@.key='value1')]";
Object tmpObj = JSONPath.extract(tmpJson, tmpPath);
assertEquals("[{\"key\":\"value1\"}]", JSON.toJSONString(tmpObj));

可以这样写

wenshao added a commit that referenced this issue Oct 21, 2022
@wangyuanqiu
Copy link
Author

不建议修改用法,新老版本不兼容,而且网上的文档又没有更新,这样显得特别bug。这个是官方的文档,并没有@的用法:https://alibaba.github.io/fastjson2/jsonpath_cn

@wenshao wenshao added this to the 2.0.17 milestone Oct 24, 2022
@wenshao
Copy link
Member

wenshao commented Oct 25, 2022

https://oss.sonatype.org/content/repositories/snapshots/com/alibaba/fastjson2/fastjson2/2.0.17-SNAPSHOT/
问题已修复,请用2.0.17-SNAPSHOT版本验证,2.0.17版本可能在这个周末发布

@wangyuanqiu
Copy link
Author

2.0.17-SNAPSHOT验证过$.arr[?(key='value1')]是可以了,但是$.arr[key='value1']依然不行,我不清楚哪种才是正确的写法,因为官方文档中( https://alibaba.github.io/fastjson2/jsonpath_cn ),第一列格式是有问号,第二列的示例却没有,麻烦请修改一下文档,避免误导用户

@wenshao
Copy link
Member

wenshao commented Oct 30, 2022

@wenshao wenshao closed this as completed Oct 30, 2022
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants