Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DataType.fromJson() Error: java.lang.IllegalArgumentException: Failed to convert the JSON string 'int' to a data type

Trying to create a StructType from a JSON file to be used as schema for creating a dataframe.

JSON structure:

    {
  "type" : "struct",
  "fields" : [
    {
    "name" : "LocationID",
    "type" : "int",
    "nullable" : false,
    "metadata" : { }
  }, {
    "name" : "Boroght",
    "type" : "string",
    "nullable" : true,
    "metadata" : { }
  }, {
    "name" : "zone",
    "type" : "string",
    "nullable" : true,
    "metadata" : { }
  }, {
    "name" : "serviceZone",
    "type" : "string",
    "nullable" : true,
    "metadata" : { }
  } ]
}

Reading JSON file and creating StructType:

val schemaSource =  Source.fromFile(url.getFile).mkString
val schemaFromJson = DataType.fromJson(schemaSource).asInstanceOf[StructType]

I keep getting the error:

java.lang.IllegalArgumentException: Failed to convert the JSON string 'int' to a data type.

Not sure if it's the JSON structure or what to change there.

like image 489
Ali Avatar asked Dec 18 '25 14:12

Ali


1 Answers

Type of LocationID should be integer not int.

like image 171
Mohana B C Avatar answered Dec 20 '25 05:12

Mohana B C



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!