0

I am trying to parse the nested Json and get the list of name from the fields tag.

{
"subject": "table-name",
"version": 1,
"id": 234,
"schema": "{
           \"type\":\"record\",\"name\":\"table-name\",
           \"fields\":[
                      {\"name\":\"NAME\",\"type\":\"CHARACTER\},
                      {\"name\":\"EID\",\"type\":\"INT\"},
                      {\"name\":\"DEPARTMENT\",\"type\":\"CHARACTER\"}
                      ]
          }"
}

I checked couple of post and came up with below code. I can get the schema definition(which is fairly easy) but not able to the list of name from fields.

case class Names(name : String)
case class FieldNames(fields: List[Names])
case class ColNames(subject: String, schema: FieldNames)
implicit val formats = DefaultFormats
val sourceSchema = parse("JsonStr").extract[ColNames]
println(sourceSchema)

My idea was the part schema: FieldNames will get me the fields tag and then List[Names] will get me the list of name but all I get is empty list. If I change the schema: FieldNames with schema: String I get the schema too but I am not getting how to get the required list of name after that. Below is the current output:

ColNames(table-name,FieldNames(List()))

Update: I got it working but I ended up parsing the same Json twice which I don't feel is a best way of doing it. I would still like to know the best way to get it done. Below is my current solution:

case class Name(name : String)
case class FieldNames(fields: List[Name])
case class ColNames(subject: String, schema: String)
val sourceSchema = parse("JsonStr").extract[ColNames]
val cols= parse(sourceSchema.schema).extract[FieldNames]

Output:

FieldNames(List(Name(NAME), Name(EID), Name(DEPARTMENT)))
Explorer
  • 1,491
  • 4
  • 26
  • 67
  • Your case class for Names (shouldn't it be called "Name", BTW?) doesn't have "type". You should try adding that in. I see other inconsistencies between your code and the actual JSON. – Phasmid Jun 02 '17 at 14:10
  • @Phasmid Thanks for your comment. Does the name of case class matters? Anyways I tried your suggestion and got the same empty List. – Explorer Jun 02 '17 at 14:14
  • no, the name of the case class doesn't matter because your JSON doesn't use names. In fact, that's the problem, I think. What your JSON is showing is a Map. The schema entry is itself a Map. The fields entry of that is a Seq (List) of more Maps. Your classes need to model that same structure. – Phasmid Jun 02 '17 at 14:20
  • I just tried `parse("JsonStr").extract[Map[String,Any]]`, Ref: https://stackoverflow.com/questions/29908297/how-can-i-convert-a-json-string-to-a-scala-map It failed with exception: >Exception in thread "main" org.json4s.package$MappingException: No information known about type – Explorer Jun 02 '17 at 14:33
  • `case class ColNames(subject: String, schema: Value)` – Reactormonk Jun 02 '17 at 15:32

0 Answers0