1

how can I parse this kind of json with spray-json? How does the guid object go into a case class or do I need to write some kind of custom reader?

{
    "referredEntities":{
        "012e2ec1-c205-4657-81d7-1e06eac1a849":{
            "typeName":"type1",
            "attributes":{
                "owner":"dave",
                "qualifiedName":"test",
                "name":"test"...
1
  • It looks like you did not include the entire json object in your question. Please do so, or edit it down to a reasonable size that shows what you need. If this IS the entire JSON string, then your json is bad, and you need to reject it. That said, the docs for spray-json seem pretty straight-forward, and suggest: val jsonAst = source.parseJson will parse your json. Commented May 24, 2018 at 19:29

1 Answer 1

1

Assuming that your JSON object will be something similar to this:

{
    "referredEntities": {
        "012e2ec1-c205-4657-81d7-1e06eac1a849":{
            "typeName":"type1",
            "attributes":{
                "owner":"dave",
                "qualifiedName":"test",
                "name":"test"
        }
     },
    "012e2ec1-c205-4657-81d7-1e06ea1234":{
            "typeName":"type2",
            "attributes":{
                "owner":"nicky",
                "qualifiedName":"test1",
                "name":"test22"
        }
     }}
}

Consider the following solution:

import spray.json._

val source = """{
               |    "referredEntities":{
               |        "012e2ec1-c205-4657-81d7-1e06eac1a849":{
               |            "typeName":"type1",
               |            "attributes":{
               |                "owner":"dave",
               |                "qualifiedName":"test",
               |                "name":"test"
               |        }
               |     },
               |    "012e2ec1-c205-4657-81d7-1e06ea1234":{
               |            "typeName":"type1",
               |            "attributes":{
               |                "owner":"dave",
               |                "qualifiedName":"test",
               |                "name":"test"
               |        }
               |     }}}""".stripMargin

val jsonAst = source.parseJson

case class Attributes(owner: String, qualifiedName: String, name: String)
case class Entity(typeName: String, attributes: Attributes)
case class ReferredEntity(entityUUID: String, entity: Entity)
case class Source(referredEntities: Seq[ReferredEntity])

object MyJsonProtocol extends  DefaultJsonProtocol {
  implicit val attributesFmt = jsonFormat3(Attributes)
  implicit val entityFmt = jsonFormat2(Entity)

  implicit object SourceJsonFormat extends RootJsonReader[Source] {
    def read(value: JsValue) = value.asJsObject.getFields("referredEntities") match {
      case Seq(obj: JsObject) => Source(obj.fields.toSeq.map(item => ReferredEntity(item._1, entityFmt.read(item._2))))
    }
  }
}

import MyJsonProtocol._

SourceJsonFormat.read(jsonAst)
Sign up to request clarification or add additional context in comments.

Comments

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.