Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions core/src/main/java/com/alibaba/fastjson2/JSONPathParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -860,8 +860,10 @@ JSONPathSegment parseFilter() {
filterNests--;
segment = parseFilterRest(segment);
}
if (!jsonReader.nextIfMatch(')')) {
throw new JSONException(jsonReader.info("jsonpath syntax error"));
if (parentheses) {
if (!jsonReader.nextIfMatch(')')) {
throw new JSONException(jsonReader.info("jsonpath syntax error"));
}
}

return segment;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.alibaba.fastjson2.jsonpath;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.alibaba.fastjson2.JSONPath;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;

public class JSONPath_in_filter_issue3997 {
@Test
public void testInFilterAfterOtherFilter() {
JSONArray riskRequests = JSONArray.of(
JSONObject.of("requestRiskType", "05", "customerType", "01", "customerName", "Alice"),
JSONObject.of("requestRiskType", "05", "customerType", "04", "customerName", "Bob"),
JSONObject.of("requestRiskType", "05", "customerType", "02", "customerName", "Carol"),
JSONObject.of("requestRiskType", "06", "customerType", "01", "customerName", "Dave")
);
JSONObject root = JSONObject.of("riskRequests", riskRequests);

Object result = JSONPath.of("$.riskRequests[?(@.requestRiskType=='05' && @.customerType in ('01','04'))].customerName")
.eval(root);
assertEquals("[\"Alice\",\"Bob\"]", JSON.toJSONString(result));
}

@Test
public void testInFilterBeforeOtherFilter() {
JSONArray riskRequests = JSONArray.of(
JSONObject.of("requestRiskType", "05", "customerType", "01", "customerName", "Alice"),
JSONObject.of("requestRiskType", "05", "customerType", "04", "customerName", "Bob"),
JSONObject.of("requestRiskType", "05", "customerType", "02", "customerName", "Carol"),
JSONObject.of("requestRiskType", "06", "customerType", "01", "customerName", "Dave")
);
JSONObject root = JSONObject.of("riskRequests", riskRequests);

Object result = JSONPath.of("$.riskRequests[?(@.customerType in ('01','04') && @.requestRiskType=='05')].customerName")
.eval(root);
assertEquals("[\"Alice\",\"Bob\"]", JSON.toJSONString(result));
}
}