3

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 Answer 1

2

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))
Sign up to request clarification or add additional context in comments.

1 Comment

Excellent that worked! Didn;t write the whole problem here - created a new question for it - my bad: stackoverflow.com/questions/68984028/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.