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]json字段有多个数组时,解析arr1数组的字段值时得到了arr2数组的结果 #1130

Closed
jindabit opened this issue Feb 21, 2023 · 10 comments
Labels
bug Something isn't working fixed
Milestone

Comments

@jindabit
Copy link

问题描述

利用fastjson2解析对应的数组字段不能得到期望结果

环境信息

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

重现步骤

如何操作可以重现该问题:
解析json字段 "{"arr1":["a"],"numeric":1,"arr2":["b"]}",期望得到数组arr2中的第一个值b,但输入arr2[0]后,得到的是arr1数组中的值a。

//可在此输入示例代码
 String raw = "{\"arr1\":[\"a\"],\"numeric\":1,\"arr2\":[\"b\"]}";
    instance.eval(raw, "$.arr2[0]");
    Mockito.verify(collector, Mockito.times(1)).collect(Row.of("b"));

### 期待的正确结果
期待得到正确的数组字段解析结果
@jindabit jindabit added the bug Something isn't working label Feb 21, 2023
@jindabit jindabit changed the title [BUG] [BUG]json字段有多个数组时,解析arr1数组的字段值时得到了arr2数组的结果 Feb 21, 2023
wenshao added a commit that referenced this issue Feb 22, 2023
@wenshao
Copy link
Member

wenshao commented Feb 22, 2023

        String raw = "{\"arr1\":[\"a\"],\"numeric\":1,\"arr2\":[\"b\"]}";
        Object result = JSONPath.eval(raw, "$.arr2[0]");
        assertEquals("b", result);

我测试没发现问题啊

@jindabit
Copy link
Author

jindabit commented Feb 23, 2023

补了几个测试用例;case2和case5是正常的,但是case3和case4测下来有问题

@Test
 public void testArrayParseCase2() {

   String raw = "{\"arr1\":[\"a\"],\"numeric\":1,\"arr2\":[\"b\"]}";
   Assert.assertEquals("b", ((String) JSONPath.extract(raw, "$.arr2[0]")));
   Assert.assertEquals("b", ((String) JSONPath.eval(raw, "$.arr2[0]")));
 }

 @Test
 public void testArrayParseCase3() {
   //expected b, actually a
   String raw = "{\"arr1\":[\"a\"],\"numeric\":1,\"arr2\":[\"b\"]}";
   Assert.assertEquals("b", ((JSONArray) JSONPath.extract(raw, "$.arr2[*][0]")));
   Assert.assertEquals("b", ((JSONArray) JSONPath.eval(raw, "$.arr2[*][0]")));
   
 }

 @Test
 public void testArrayParseCase4() {
   //expected b, actually a
   String raw = "{\"arr1\":[\"a\"],\"numeric\":1,\"arr2\":[\"b\"]}";
   String[] paths = new String[] {"$.arr2[0]"};

   Type[] types = new Type[paths.length];
   Arrays.fill(types, String.class);
   JSONPath path = JSONPath.of(paths, types);
   Object[] results = (Object[]) path.extract(raw);
   Assert.assertEquals("b", results[0]);
   
 }

@Test
 public void testArrayParseCase5() {
   String raw = "{\"arr1\":[\"a\"],\"numeric\":1,\"arr2\":[\"b\"]}";
   String[] paths = new String[] {"$.arr2[0]", "$.arr1[0]"};

   Type[] types = new Type[paths.length];
   Arrays.fill(types, String.class);
   JSONPath path = JSONPath.of(paths, types);
   Object[] results = (Object[]) path.extract(raw);
   Assert.assertEquals("b", results[0]);
   Assert.assertEquals("a", results[1]);
 }

@wenshao
Copy link
Member

wenshao commented Mar 1, 2023

https://oss.sonatype.org/content/repositories/snapshots/com/alibaba/fastjson2/fastjson2/2.0.25-SNAPSHOT/
问题已修复,请帮忙用2.0.25-SNAPSHOT版本验证,2.0.25版本预计在3月中旬发布。

可以说下你使用场景么?

@lhaox
Copy link
Contributor

lhaox commented Mar 4, 2023

@wenshao  也许JSONPathTypedMultiIndexes还存在缺陷,补充case

@Test
public void testArrayParseCase6() {
    String raw = "{\"arr1\":[\"a\"],\"numeric\":1,\"arr2\":[\"a\"]}";
    JSONPath path = JSONPath.of(new String[]{"$.arr2[0]", "$.arr2[0]"}, new Type[]{String.class, String.class});
    Object[] results = (Object[]) path.extract(raw);
    assertArrayEquals(new Object[]{"a", "a"}, results);
}

运行结果:
org.opentest4j.AssertionFailedError: array contents differ at index [1],
Expected :a
Actual :null

@wenshao
Copy link
Member

wenshao commented Mar 4, 2023

我对你的PR做了一个性能优化,在没有重复index时,不做额外的计算。

可以分享下你的使用场景么?我对大家怎么用这个功能需要反馈

@wenshao wenshao added the fixed label Mar 4, 2023
@wenshao wenshao added this to the 2.0.25 milestone Mar 4, 2023
@lhaox
Copy link
Contributor

lhaox commented Mar 4, 2023

我对你的PR做了一个性能优化,在没有重复index时,不做额外的计算。

可以分享下你的使用场景么?我对大家怎么用这个功能需要反馈

@wenshao 感谢您对我这个PR斧正。
我并没有在实际生产中使用到这个功能,只是对fastjson2感兴趣,测试这个issue的作者提供的case时发现了问题

@Leoyzen
Copy link

Leoyzen commented Mar 6, 2023

@wenshao 我们主要在大数据场景下,通过 fastjson2 实现 json udf 来加速 json 的解析。所以相关的 issue 会比较多。
类似于 json_path 函数、get_json_object、from_json 等hive 、flink 下比较常用的 json udf 用法。

数据场景就是常见的日志解析,大 json 中获取少量的字段出来进行处理。

@Leoyzen
Copy link

Leoyzen commented Mar 6, 2023

另外之前的一个 issue 下的补充,貌似还没解决,也是相关的#1070

// 测试不通过,返回的是嵌套数据 "[[[{\"a\":1},{\"a\":2}],[{\"a\":3}]]]";
String raw = "[[{\"a\":1},{\"a\":2}],[{\"a\":3}]]";
Assert.assertEquals("[[{\"a\":1},{\"a\":2}],[{\"a\":3}]]",
                ((JSONArray) JSONPath.extract(raw, "$")).toJSONString());
// 测试通过
Assert.assertEquals("[[{\"a\":1},{\"a\":2}],[{\"a\":3}]]";,
                ((JSONArray) JSONPath.extract(raw, "$[*]")).toJSONString());

@wenshao wenshao modified the milestones: 2.0.25, 2.0.26 Mar 12, 2023
wenshao added a commit that referenced this issue Mar 14, 2023
@wenshao
Copy link
Member

wenshao commented Mar 14, 2023

@Leoyzen
https://oss.sonatype.org/content/repositories/snapshots/com/alibaba/fastjson2/fastjson2/2.0.26-SNAPSHOT/
问题已修复,请帮忙用2.0.26-SNAPSHOT版本验证,2.0.26版本预计在4月中旬发布。

@wenshao
Copy link
Member

wenshao commented Mar 19, 2023

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

@wenshao wenshao closed this as completed Mar 19, 2023
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

4 participants