danny.lesnik
danny.lesnik

Reputation: 18639

Spray JSON - parse only part of the JSON response

I have the following Json response from the server where samples object contains

{  
   "op":{  
      "samples":{ 
          "name1" :[0,0,0,0,0,0,0],
          "name2" :[0,0,0,0,0,0,0],
          "name3" :[0,0,0,0,0,0,0],

            //About 100 more names

          "name99" :[1,2,3,4,5,6,7],
          "name100" :[0,0,0,0,0,0,0],

      },
      "samplesCount":60,
      "isPersistent":true,
      "lastTStamp":1415619627689,
      "interval":1000
   },
   "hot_keys":[  
      {  
         "name":"counter::F03E91E2A4B9C25F",
         "ops":0.11010372549516878
      }
        //About 40 objects
   ]
}

I need only some parts of this result.

The following properties are required:

name1, name23, timeStamp and isPersistant  

So I created the following case classes and its implicit parsers:

 case class Samples(name1[Int],name23[Int])
  case class Op(samples:Samples,lastTStamp:String,isPersistent:Boolean)
  case class BucketStatisticResponse(op:Op)

  object BucketStatisticJsonProtocol extends DefaultJsonProtocol {
    implicit val samplesFormat = jsonFormat2(Samples)
    implicit val opFormat = jsonFormat3(Op)
    implicit val bucketStatisticFormat= jsonFormat1(BucketStatisticResponse)
  }

but i'm getting the following error:

spray.httpx.PipelineException: MalformedContent(Expected String as JsString, but got 1069547520,Some(spray.json.DeserializationException: Expected String as JsString, but got 1069547520))

Can you please help?

Upvotes: 1

Views: 2073

Answers (1)

grotrianster
grotrianster

Reputation: 2468

Error message says that spray-json was expecting String but got sth else, it seems you need to define "lastTStamp" in Op class as Long, like this:

case class Op(samples:Samples, lastTStamp:Long, isPersistent:Boolean)

Upvotes: 2

Related Questions