Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

json4s parsing StructType #1028

Open
cwang9208 opened this issue Mar 13, 2022 · 0 comments
Open

json4s parsing StructType #1028

cwang9208 opened this issue Mar 13, 2022 · 0 comments

Comments

@cwang9208
Copy link

cwang9208 commented Mar 13, 2022

json4s version

All versions

scala version

2.12

jdk version

java 8

I'm trying to leverage json4s for parsing the StructType from Spark.

scala> import org.apache.spark.sql.types._

scala> case class Person(name: StructType)
defined class Person

scala> import org.json4s._
import org.json4s._

scala> import org.json4s.jackson.JsonMethods._
import org.json4s.jackson.JsonMethods._

scala> implicit val formats: Formats = DefaultFormats

scala> val json = parse("""  {"name": []} """)

scala> json.extract[Person]

org.json4s.package$MappingException: unknown error
  at org.json4s.Extraction$.extract(Extraction.scala:43)
  at org.json4s.ExtractableJsonAstNode.extract(ExtractableJsonAstNode.scala:21)
  ... 55 elided
Caused by: java.util.NoSuchElementException: head of empty list
  at scala.collection.immutable.Nil$.head(List.scala:431)
  at scala.collection.immutable.Nil$.head(List.scala:428)
  at org.json4s.Extraction$CollectionBuilder.<init>(Extraction.scala:407)
  at org.json4s.Extraction$$anonfun$extract$5.apply(Extraction.scala:382)
  at org.json4s.Extraction$$anonfun$extract$5.apply(Extraction.scala:382)
  at org.json4s.Extraction$.customOrElse(Extraction.scala:606)
  at org.json4s.Extraction$.extract(Extraction.scala:382)
  at org.json4s.Extraction$ClassInstanceBuilder.org$json4s$Extraction$ClassInstanceBuilder$$buildCtorArg(Extraction.scala:514)
  at org.json4s.Extraction$ClassInstanceBuilder$$anonfun$15.apply(Extraction.scala:546)
  at org.json4s.Extraction$ClassInstanceBuilder$$anonfun$15.apply(Extraction.scala:546)
  at scala.collection.TraversableLike$$anonfun$map$1.apply(TraversableLike.scala:234)
  at scala.collection.TraversableLike$$anonfun$map$1.apply(TraversableLike.scala:234)
  at scala.collection.mutable.ResizableArray$class.foreach(ResizableArray.scala:59)
  at scala.collection.mutable.ArrayBuffer.foreach(ArrayBuffer.scala:48)
  at scala.collection.TraversableLike$class.map(TraversableLike.scala:234)
  at scala.collection.AbstractTraversable.map(Traversable.scala:104)
  at org.json4s.Extraction$ClassInstanceBuilder.instantiate(Extraction.scala:546)
  at org.json4s.Extraction$ClassInstanceBuilder.result(Extraction.scala:597)
  at org.json4s.Extraction$$anonfun$extract$6.apply(Extraction.scala:400)
  at org.json4s.Extraction$$anonfun$extract$6.apply(Extraction.scala:392)
  at org.json4s.Extraction$.customOrElse(Extraction.scala:606)
  at org.json4s.Extraction$.extract(Extraction.scala:392)
  at org.json4s.Extraction$.extract(Extraction.scala:39)
  ... 56 more

StructType in Spark is actually a Spark case class with an array as parameter:

case class StructType(fields: Array[StructField]) extends DataType with Seq[StructField]

I just passed an empty list in json, I think that should work. Can any one help me fix this problem?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

1 participant