MaxGekk commented on code in PR #48428:
URL: https://github.com/apache/spark/pull/48428#discussion_r1797781473
##########
sql/catalyst/src/main/java/org/apache/spark/sql/catalyst/expressions/json/JsonExpressionUtils.java:
##########
@@ -55,4 +58,32 @@ public static Integer lengthOfJsonArray(UTF8String json) {
return null;
}
}
+
+ public static GenericArrayData jsonObjectKeys(UTF8String json) {
+ // return null for `NULL` input
+ if (json == null) {
+ return null;
+ }
+ try (JsonParser jsonParser =
+ CreateJacksonParser.utf8String(SharedFactory.jsonFactory(), json)) {
+ // return null if an empty string or any other valid JSON string is
encountered
+ if (jsonParser.nextToken() == null || jsonParser.currentToken() !=
JsonToken.START_OBJECT) {
+ return null;
+ }
+ // Parse the JSON string to get all the keys of outermost JSON object
+ List<UTF8String> arrayBufferOfKeys = new ArrayList<>();
+
+ // traverse until the end of input and ensure it returns valid key
+ while (jsonParser.nextValue() != null && jsonParser.currentName() !=
null) {
+ // add current fieldName to the ArrayBuffer
+ arrayBufferOfKeys.add(UTF8String.fromString(jsonParser.currentName()));
+
+ // skip all the children of inner object or array
+ jsonParser.skipChildren();
+ }
+ return new GenericArrayData(arrayBufferOfKeys.toArray());
+ } catch (IOException e) {
Review Comment:
Why don't you handle `JsonProcessingException`?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]