0

Want to compare the first json string with the other 2 json string. First the keys should match . If they match , then compare the nested key and values.

 val of1 = "{\"keyA\":{\"1\":13,\"0\":202}}"
  val of2 = "{\"keyA\":{\"1\":12,\"0\":201}}"
  val of3 = "{\"keyB\":{\"1\":12}}"

Should throw Error for key mismatch.

 val of1 = "{\"keyA\":{\"1\":13,\"0\":202}}"
  val of2 = "{\"keyA\":{\"1\":12,\"0\":201}}"
  val of2 = "{\"keyA\":{\"1\":11,\"0\":200}}"

This should return true, as both keys match and also sub keys 1 and 0 have more values than sub key of json 2 and json 3.The numbers are Long values.

Please help.

Below is my try.

 import com.fasterxml.jackson.databind.ObjectMapper
  import com.fasterxml.jackson.module.scala.DefaultScalaModule
  import com.fasterxml.jackson.module.scala.experimental.ScalaObjectMapper

  val of1 = "{\"keyA\":{\"1\":13,\"0\":202}}"
  val of2 = "{\"keyA\":{\"1\":12,\"0\":201}}"
  val of3 = "{\"keyB\":{\"1\":12}}"


  def OffsetComparator(json1: String, json2: String, json3:String): Boolean = {

    val mapper = new ObjectMapper() with ScalaObjectMapper
    
    mapper.registerModule(DefaultScalaModule)
    
    val jsonObj1 = mapper.readValue(json1, classOf[Map[String, Map[String, Long]]])
    val jsonObj2 = mapper.readValue(json2, classOf[Map[String, Map[String, Long]]])
    val jsonObj3 = mapper.readValue(json3, classOf[Map[String, Map[String, Long]]])


//Trying to get the key and compare first
    val mapA = jsonObj1.keySet.foreach(i=>jsonObj1.keySet(i).toString)
    val mapB = jsonObj2.keySet
    val mapC = jsonObj3.keySet



   println( (jsonObj1.keySet == jsonObj3.keySet) )

      if (mapA.keySet != mapB.keySet || mapA.keySet != mapC.keySet) throw new Exception("partitions mismatch")

      mapA.keys.forall(k => (mapA(k).asInstanceOf[Long] > mapB(k).asInstanceOf[Long] && mapA(k).asInstanceOf[Long] > mapC(k).asInstanceOf[Long]))

// getting error :java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.Long when i am casting as Long.Not su

  }

  println(OffsetComparator(of1, of2,of3))
}

1 Answer 1

0

You can try with https://github.com/gnieh/diffson. ITs available for circe, spray-json and play-json.

Your example with Circe:

import diffson._
import diffson.lcs._
import diffson.jsonpatch.lcsdiff._

import io.circe._
import diffson.circe._
import diffson.jsonpatch._
import io.circe.parser._

val decoder = Decoder[JsonPatch[Json]]
val encoder = Encoder[JsonPatch[Json]]

implicit val lcs = new Patience[Json]

val json1 = parse(of1)
val json2 = parse(of2)

val patch =
  for {
    json1 <- json1
    json2 <- json2
  } yield diff(json1, json2)

print(patch)

That gives:

Right(JsonPatch(List(Replace(Chain(Left(keyA), Left(0)),201,None), Replace(Chain(Left(keyA), Left(1)),12,None))))

take a look to see how it works https://index.scala-lang.org/gnieh/diffson/diffson-circe/4.0.3?target=_2.13

For Circe, indlude the dependence:

"org.gnieh" %% f"diffson-circe" % "4.0.3"
Sign up to request clarification or add additional context in comments.

1 Comment

I already use jackson in other modules. Would like to stick to jackson for any parsing requirements.

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.