Github user gatorsmile commented on a diff in the pull request: https://github.com/apache/spark/pull/16981#discussion_r101948378 --- Diff: sql/core/src/test/scala/org/apache/spark/sql/JsonFunctionsSuite.scala --- @@ -174,4 +174,44 @@ class JsonFunctionsSuite extends QueryTest with SharedSQLContext { .select(to_json($"struct").as("json")) checkAnswer(dfTwo, readBackTwo) } + + test("SPARK-19637 Support to_json/from_json in SQL") { + // to_json + val df1 = Seq(Tuple1(Tuple1(1))).toDF("a") + checkAnswer( + df1.selectExpr("to_json(a)"), + Row("""{"_1":1}""") :: Nil) + + val df2 = Seq(Tuple1(Tuple1(java.sql.Timestamp.valueOf("2015-08-26 18:00:00.0")))).toDF("a") + checkAnswer( + df2.selectExpr("""to_json(a, '{"timestampFormat": "dd/MM/yyyy HH:mm"}')"""), + Row("""{"_1":"26/08/2015 18:00"}""") :: Nil) + + val errMsg1 = intercept[AnalysisException] { + df2.selectExpr("""to_json(a, '{"k": [{"k": "v"}]}')""").collect + } + assert(errMsg1.getMessage.startsWith( + """The format must be '{"key": "value", ...}', but {"k": [{"k": "v"}]}""")) + + // from_json + val df3 = Seq("""{"a": 1}""").toDS() + val schema1 = new StructType().add("a", IntegerType) + checkAnswer( + df3.selectExpr(s"from_json(value, '${schema1.json}')"), + Row(Row(1)) :: Nil) + + val df4 = Seq("""{"time": "26/08/2015 18:00"}""").toDS() + val schema2 = new StructType().add("time", TimestampType) + checkAnswer( + df4.selectExpr( + s"""from_json(value, '${schema2.json}', """ + + """'{"timestampFormat": "dd/MM/yyyy HH:mm"}')"""), --- End diff -- Regarding the format of options, another way is to use the MapType. For example, ```Scala from_json(value, '${schema2.json}', map("timestampFormat", "dd/MM/yyyy HH:mm")) ``` I am not sure whether using JSON to represent options is a good way.
--- If your project is set up for it, you can reply to this email and have your reply appear on GitHub as well. If your project does not have this feature enabled and wishes so, or if the feature is enabled but not working, please contact infrastructure at infrastruct...@apache.org or file a JIRA ticket with INFRA. --- --------------------------------------------------------------------- To unsubscribe, e-mail: reviews-unsubscr...@spark.apache.org For additional commands, e-mail: reviews-h...@spark.apache.org