alibaba / alibaba/fastjson2

[BUG] JSONPath.extract

Open
#3,192 1 comment 0 reactions 0 assignees View on GitHub
bug
Dominant language
Java
Stars
4.4k
Forks
613
Avg merge
1d 22h
Merged PRs (30d)
6

Description

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

```java
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

### 重现步骤
*运行如下代码即可复现:*

```java
@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`或者`[]`。*

Contributor guide

Open the contributing guide

Research direction

Start with the test_jsonpath_array reproduction and trace JSONPath.extract through JSONPath.java and JSONPathMulti.java to JSONPathSegment.java, especially AllSegment.accept. Compare the wildcard path with the numeric-index path for null and empty arrays. Done means both forms consistently return null or [] as described, without the JSONException.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
backend
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.