Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

[BUG] JSONPath.extract #3192

Open
Deathef opened this issue Dec 5, 2024 · 1 comment
Open

[BUG] JSONPath.extract #3192

Deathef opened this issue Dec 5, 2024 · 1 comment
Labels
bug Something isn't working

Comments

@Deathef
Copy link

Deathef commented Dec 5, 2024

问题描述

在使用JSONPath处理列表数据的时候,发现当列表无数据的时候,使用$.array[*].key会报如下错误,而使用$.array[0].key(任意下标数字)都能正常返回[]

Exception in thread "main" com.alibaba.fastjson2.JSONException: TODO
	at com.alibaba.fastjson2.JSONPathSegment$AllSegment.accept(JSONPathSegment.java:1291)
	at com.alibaba.fastjson2.JSONPathMulti.extract(JSONPathMulti.java:398)
	at com.alibaba.fastjson2.JSONPath.extract(JSONPath.java:62)

环境信息

请填写以下信息:

  • OS信息: Mac OS 15.1.1
  • JDK信息: openjdk version "21.0.5" 2024-10-15 LTS
  • 版本信息:Fastjson2 2.0.53

重现步骤

运行如下代码即可复现:

@Test
public void test_jsonpath_array() {
    String json = "{ \"array\": null }";
    System.out.println(JSONPath.extract(json, "$.array[0].key", JSONPath.Feature.KeepNullValue, JSONPath.Feature.AlwaysReturnList));
    System.out.println(JSONPath.extract(json, "$.array[*].key", JSONPath.Feature.KeepNullValue, JSONPath.Feature.AlwaysReturnList));
}

期待的正确结果

当列表数据为null[]时,使用*和数字的逻辑能保持一致,返回null或者[]

@Deathef Deathef added the bug Something isn't working label Dec 5, 2024
@Deathef
Copy link
Author

Deathef commented Dec 5, 2024

在JSON中,列表数据有值的情况,就无上述问题。

@Test
public void test_jsonpath_array() {
    String json = "{ \"array\": [{\"key\":1}, {\"key\":null}, {\"key\":3}] }";
    System.out.println(JSONPath.extract(json, "$.array[*].key", JSONPath.Feature.KeepNullValue, JSONPath.Feature.AlwaysReturnList));
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant