iRakson commented on a change in pull request #28167: [SPARK-31106][SQL] 
Support is_json function
URL: https://github.com/apache/spark/pull/28167#discussion_r406318797
 
 

 ##########
 File path: 
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/jsonExpressions.scala
 ##########
 @@ -933,3 +933,58 @@ case class JsonObjectKeys(child: Expression) extends 
UnaryExpression with Codege
     new GenericArrayData(arrayBufferOfKeys.toArray)
   }
 }
+
+/**
+ * A function which checks whether the given JSON string is valid JSON string 
or not.
+ */
+@ExpressionDescription(
+  usage = """
+    _FUNC_(json_string) - Returns `true` if `json_string` is valid JSON, 
`false` if `json_string` is
+      invalid. `NULL` is returned in case of `NULL`.
+  """,
+  examples = """
+    Examples:
+      > Select _FUNC_(null);
+        NULL
+      > Select _FUNC_('{"key" : "valid JSON"}');
+        true
+      > Select _FUNC_('{"invalid JSON"}');
+        false
+  """,
+  since = "3.1.0"
+)
+case class IsJson(child: Expression) extends UnaryExpression with 
CodegenFallback
+  with ExpectsInputTypes {
+
+  override def inputTypes: Seq[DataType] = Seq(StringType)
+
+  override def dataType: DataType = BooleanType
+
+  override def prettyName: String = "is_json"
+
+  override def nullable: Boolean = true
+
+  override def eval(input: InternalRow): Any = {
+    val json = child.eval(input).asInstanceOf[UTF8String]
+    // return null for null input
+    if (json == null) {
+      return null
+    }
+
+    try {
+      
Utils.tryWithResource(CreateJacksonParser.utf8String(SharedFactory.jsonFactory, 
json)) {
+        parser => {
+          // parse the JSON string
+          while (parser.nextToken() != null) {
+            parser.skipChildren()
 
 Review comment:
   no, nested fields are checked by `JsonParser` as well.

----------------------------------------------------------------
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.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to