Home > Net >  JsonPath is converting colon(:) into equal to(=) while reading each element of an array
JsonPath is converting colon(:) into equal to(=) while reading each element of an array

Time:01-04

I am using "com.jayway.jsonpath" % "json-path" % "2.7.0" for parsing an incoming json message in our API build on akka-http written in scala. The incoming message can have array of records and for each record, some actions will be performed. While trying to access each record from that array, colon(:) is being converted to equals to(=). Below is the code:

val conf= com.jayway.jsonpath.Configuration.defaultConfiguration.addOptions(Option.DEFAULT_PATH_LEAF_TO_NULL)
val doc = JsonPath.using(conf).parse(input)
val arrLen = doc.read("$.records.length()").toString.toInt
              for(i <- 0 until( arrLen)){
                val str = "$.records[" i "]"
                val rec = doc.read(str).toString
                println(rec)
              }

The payload which I am trying to parse:

{"schema_id":"1","records":[{"application":{"applicationRevision":{"string":"27904"},"applicationVersion":"india","id":"132231","name":"appTest"},"guest":{"session":"12bvg","systemTime":"2021-08-24T21:19:13.282Z","visitorId":"abc"}}]}

This is what I am getting after accessing element from the records array:

{application={applicationRevision={string=27904}, applicationVersion=india, id=132231, name=appTest}, guest={session=12bvg, systemTime=2021-08-24T21:19:13.282Z, visitorId=abc}}

All the colons(:) have been converted into equals(=). How can we prevent this to happen so that the payload remain intact without colon to equals conversion?

CodePudding user response:

It is pretty easy using play, just a single line solution:

(Json.parse(input) \ "records").as[List[JsValue]].map(ele => ele.toString()).map{rec =>
            //business logic
          }

Add play in your build.sbt(in my case) like this:

"com.typesafe.play" %% "play" % "2.8.16"
  • Related