3

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.

1
  • The type should be integer instead of int Commented Aug 30, 2021 at 12:33

1 Answer 1

3

Type of LocationID should be integer not int.

Sign up to request clarification or add additional context in comments.

1 Comment

Using integer, but still getting the same error

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.