Parse json list to two list types by field value in Scala circe

857 views Asked by At

I got given (example) json:

{
    "version": 1.1,
    "author": "XYZ",
    "elements": [{
            "type": "nodeX",
            "id": 1,
            "a": 1,
            "b": 2
        },
        {
            "type": "nodeX",
            "id": 2,
            "a": 1,
            "b": 2
        },

        ...

        {
            "type": "nodeX",
            "id": 13214,
            "a": 1,
            "b": 2
        },
        {
            "type": "nodeY",
            "id": 1,
            "c": [
                "qaz",
                "wsx"
            ]
        },
        {
            "type": "nodeY",
            "id": 2,
            "c": [
                "qaz",
                "wsx"
            ]
        },

        ...

        {
            "type": "nodeY",
            "id": 3,
            "c": [
                "qaz",
                "wsx"
            ]
        }
    ]
}

Elements list always contain objects with 2 possibility:

  • type "nodeX" and properties: id, a and b.
  • type "nodeY" and properties: id and c.

I want to get two lists of the given classes:

case class NodeX(val id:Long, val a:Long, val b:Long)
case class NodeY(val id:Long, val c:List[String])

I have tried circe (Scala library) to parse this json to classes:

case class Element(val 'type':String, val id:Long, val a:Option[Long],val b:Option[Long], val c:Option[List[String]])
case class MyJson(val version:Double, val author:String, val elements:List[Element])

but unfortunately i got list of objects Elements with optional fields.
Currently i am using this as workaround:

val elements = // MyJson.elements
for (elem <- elements)
    elem match {
      case Element("nodeX", _,_,_,_) => //here convert to NodeX and add to list List[NodeX]
      case Element("nodeY", _,_,_,_) => //here convert to NodeY and add to list List[NodeY]
    }

I am looking for better solution, faster solution because list in this json contains never less than 70k elements.

Thanks in advance :)

2

There are 2 answers

0
WeiChing 林煒清 On

another way is dynamic Scala in Circe + Monocle that give you unsafe flexibility of dynamic language:

libraryDependencies += "io.circe" %% "circe-optics" % circeVersion
import io.circe.optics.JsonPath._
import io.circe.parser

val json = parser.parse(jsonString).right.get

case class NodeX(val id:Long, val a:Long, val b:Long)
case class NodeY(val id:Long, val c:List[String]) 

val nodexs = root.elements.each
    .filter(root.`type`.string.getOption(_).contains("nodeX"))
    .as[NodeX].getAll(json)
//res: List[NodeX] = List(NodeX(1L, 1L, 2L), NodeX(2L, 1L, 2L))
val nodeys = root.elements.each
    .filter(root.`type`.string.getOption(_).contains("nodeY"))
    .as[NodeY].getAll(json)
//res: List[NodeY] = List(NodeY(3L, List("qaz", "wsx")))

And I believe circe-generic auto mode can do the same what described in Andriy Plokhotnyuk's answer.

0
Andriy Plokhotnyuk On

If it is allowed for you to define NodeX and NodeY classes as ADT with sealed trait, that it can be easy parsed by jsoniter-scala.

Add the library to your dependencies list

libraryDependencies ++= Seq(
  "com.github.plokhotnyuk.jsoniter-scala" %% "jsoniter-scala-core" % "0.29.2" % Compile, 
  "com.github.plokhotnyuk.jsoniter-scala" %% "jsoniter-scala-macros" % "0.29.2" % Provided // required only in compile-time
)

Define your case classes:

sealed trait Node
final case class NodeX(val id:Long, val a:Long, val b:Long) extends Node
final case class NodeY(val id:Long, val c:List[String]) extends Node

case class MyJson(val version:Double, val author:String, val elements:List[Node])

Generate codec for the root case class and use it

import java.io._
import com.github.plokhotnyuk.jsoniter_scala.macros._
import com.github.plokhotnyuk.jsoniter_scala.core._

val myJsonCodec = JsonCodecMaker.make[MyJson](CodecMakerConfig())

val myJson = {
  val fin = new FileInputStream("/tmp/my.json")
  try readFromStream(codec, fin)
  finally fin.close()
}