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 自动过滤 null 属性 #1159

Closed
holate opened this issue Feb 28, 2023 · 5 comments
Closed

[BUG] JSONPath 自动过滤 null 属性 #1159

holate opened this issue Feb 28, 2023 · 5 comments
Labels
bug Something isn't working fixed
Milestone

Comments

@holate
Copy link

holate commented Feb 28, 2023

问题描述

简要描述您碰到的问题。

JSONPath 自动删除 null 属性

环境信息

请填写以下信息:

  • OS信息: [e.g.:CentOS 8.4.2105 4Core 3.10GHz 16 GB]
  • JDK信息: [e.g.:Openjdk 1.8.0_312]
  • 版本信息:2.0.24

重现步骤

如何操作可以重现该问题:

    private void test() {
        JSONObject jsonObject = JSON.parseObject(s);
        String key = "data[*].title";

        Collection eval = (Collection) JSONPath.of(key, Collection.class).eval(jsonObject);
        System.out.println(eval);
    }

    String s = "{\n"
        + "    \"data\": [\n"
        + "        {\n"
        + "            \"title\": \"title_1\"\n"
        + "        },\n"
        + "        {\n"
        + "            \"title\": null\n"
        + "        },\n"
        + "        {\n"
        + "            \"title\": \"title_3\"\n"
        + "        }\n"
        + "    ]\n"
        + "}";

期待的正确结果

对您期望发生的结果进行清晰简洁的描述。

[
"title_1",
null,
"title_3"
]

相关日志输出

请复制并粘贴任何相关的日志输出。

输出结果:["title_1","title_3"]

附加信息

如果你还有其他需要提供的信息,可以在这里填写(可以提供截图、视频等)。

image

@holate holate added the bug Something isn't working label Feb 28, 2023
@wenshao
Copy link
Member

wenshao commented Mar 5, 2023

在mysql8中:

select json_extract('{"data":[{"title":"title_1"},{},{"title":"title_3"}]}', '$.data[*].title')

返回结果是这个:

["title_1", "title_3"]

@holate
Copy link
Author

holate commented Mar 9, 2023

在 fastjson1 中具有参数 ignoreNullValue,可以保留 null 值,是否可以保留这一参数?

    public static Object eval(Object rootObject, String path, boolean ignoreNullValue) {
        JSONPath jsonpath = compile(path, ignoreNullValue);
        return jsonpath.eval(rootObject);
    }

https://github.com/alibaba/fastjson/blob/master/src/main/java/com/alibaba/fastjson/JSONPath.java

@wenshao wenshao added this to the 2.0.26 milestone Mar 14, 2023
@wenshao wenshao added the fixed label Mar 16, 2023
@wenshao
Copy link
Member

wenshao commented Mar 16, 2023

JSONObject jsonObject = JSON.parseObject(s);
String key = "data[*].title";

JSONPath jsonPath = JSONPath.of(key, Collection.class, JSONPath.Feature.KeepNullValue);
Collection eval = (Collection) jsonPath.eval(jsonObject);
assertEquals("[\"title_1\",null,\"title_3\"]", eval.toString());

https://oss.sonatype.org/content/repositories/snapshots/com/alibaba/fastjson2/fastjson2/2.0.26-SNAPSHOT/
增加了JSONPath.Feature.KeepNullValue,请帮忙用2.0.26-SNAPSHOT版本验证

@holate
Copy link
Author

holate commented Mar 17, 2023

验证通过,感谢

@holate holate closed this as completed Mar 17, 2023
@wenshao
Copy link
Member

wenshao commented Mar 19, 2023

https://github.com/alibaba/fastjson2/releases/tag/2.0.26
新版本已发布,请用新版本

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

No branches or pull requests

2 participants