DataType.fromJSON() - java.lang.IllegalArgumentException: Failed to convert the JSON string <> to a data type

Viewed 478

Trying to create a [StructType] from a JSON string.

val test = "{\"test\":\"validate\"}"
println( "datatype: " + DataType.fromJson(test) )

Keep getting error:

java.lang.IllegalArgumentException: Failed to convert the JSON string '{"test":"validate"}' to a data type.

Not sure what's the problem here..

1 Answers

You should define the JSON as shown below.

val test = """{
             |   "type":"struct",
             |   "fields":[
             |      {
             |         "name":"test",
             |         "type":"string",
             |         "nullable":true,
             |         "metadata":{
             |            
             |         }
             |      }
             |   ]
             |}""".stripMargin

println("datatype: " + DataType.fromJson(test))

//datatype: StructType(StructField(test,StringType,true))
Related