Skip to content

Commit

Permalink
addessed comments
Browse files Browse the repository at this point in the history
Signed-off-by: 14yapkc1 <[email protected]>
  • Loading branch information
kenrickyap committed Jan 27, 2025
1 parent 0f0b8d4 commit f030057
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 18 deletions.
20 changes: 3 additions & 17 deletions core/src/main/java/org/opensearch/sql/utils/JsonUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,9 @@
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.jayway.jsonpath.Configuration;
import com.jayway.jsonpath.InvalidJsonException;
import com.jayway.jsonpath.InvalidPathException;
import com.jayway.jsonpath.JsonPath;
import com.jayway.jsonpath.Option;
import com.jayway.jsonpath.PathNotFoundException;
import java.util.LinkedHashMap;
import java.util.LinkedList;
Expand Down Expand Up @@ -60,25 +58,13 @@ public static ExprValue extractJson(ExprValue json, ExprValue path) {
String jsonString = json.stringValue();
String jsonPath = path.stringValue();

if (jsonString.equals("")) {
if (jsonString.isEmpty()) {
return LITERAL_NULL;
}

try {
Configuration config = Configuration.builder().options(Option.AS_PATH_LIST).build();
List<String> resultPaths = JsonPath.using(config).parse(jsonString).read(jsonPath);

List<ExprValue> elements = new LinkedList<>();
for (String resultPath : resultPaths) {
Object result = JsonPath.parse(jsonString).read(resultPath);
elements.add(ExprValueUtils.fromObjectValue(result));
}

if (elements.size() == 1) {
return elements.get(0);
} else {
return new ExprCollectionValue(elements);
}
Object results = JsonPath.parse(jsonString).read(jsonPath);
return ExprValueUtils.fromObjectValue(results);
} catch (PathNotFoundException e) {
return LITERAL_NULL;
} catch (InvalidPathException e) {
Expand Down
2 changes: 1 addition & 1 deletion docs/user/ppl/functions/json.rst
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ ____________
Description
>>>>>>>>>>>

Usage: `json_extract(doc, path [, path]...)` Extracts a json value or scalar from a json document based on the path(s) specified.
Usage: `json_extract(doc, path)` Extracts a json value or scalar from a json document based on the path(s) specified.

Argument type: STRING, STRING

Expand Down

0 comments on commit f030057

Please sign in to comment.