panbingkun commented on code in PR #48428:
URL: https://github.com/apache/spark/pull/48428#discussion_r1798021277


##########
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:
   If written as one of the following two styles:
   
   - A.
   <img width="951" alt="image" 
src="https://github.com/user-attachments/assets/d399880c-1cbd-44f8-ad04-19cfd50e5bfa";>
   
   - B.
   <img width="719" alt="image" 
src="https://github.com/user-attachments/assets/0b8a7c71-9dfc-4063-bee2-dd25f51096bb";>
   
   - Because:
   `JsonProcessingException extends JacksonException` and `JacksonException 
extends java.io.IOException`
   
   - So, Based on my knowledge that catch `java.io.IOException` should already 
be enough to achieve the goal.



-- 
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]

Reply via email to