簡體   English   中英

如何找到兩個 JSON 文件之間的差異/不匹配?

[英]How to find the difference/mismatch between two JSON file?

我有兩個 json 文件,一個是預期的 json,另一個是 GET API 調用的結果。 我需要比較並找出文件中的不匹配。

預期 Json:

{
  "array": [
    1,
    2,
    3
  ],
  "boolean": true,
  "null": null,
  "number": 123,
  "object": {
    "a": "b",
    "c": "d",
    "e": "f"
  },
  "string": "Hello World"
}

實際 Json 響應:

{
  "array": [
    1,
    2,
    3
  ],
  "boolean": true,
  "null": null,
  "number": 456,
  "object": {
    "a": "b",
    "c": "d",
    "e": "f"
  },
  "string": "India"
}

實際上有兩個不匹配:收到的號碼是 456,字符串是印度。

有沒有辦法比較這兩個不匹配作為結果。

這需要在 gatling/scala 中實現。

例如,您可以使用play-json 庫並遞歸遍歷這兩個 JSON。 對於下一個輸入(比您的輸入更復雜):

剩下:

{
  "array" : [ 1, 2, 4 ],
  "boolean" : true,
  "null" : null,
  "number" : 123,
  "object" : {
    "a" : "b",
    "c" : "d",
    "e" : "f"
  },
  "string" : "Hello World",
  "absent-in-right" : true,
  "different-types" : 123
}

正確的:

{
  "array" : [ 1, 2, 3 ],
  "boolean" : true,
  "null" : null,
  "number" : 456,
  "object" : {
    "a" : "b",
    "c" : "d",
    "e" : "ff"
  },
  "string" : "India",
  "absent-in-left" : true,
  "different-types" : "YES"
}

它產生這個 output:

Next fields are absent in LEFT: 
     *\absent-in-left
Next fields are absent in RIGHT: 
     *\absent-in-right
'*\array\(2)' => 4 != 3
'*\number' => 123 != 456
'*\object\e' => f != ff
'*\string' => Hello World != India
Cannot compare JsNumber and JsString in '*\different-types'

代碼:

val left = Json.parse("""{"array":[1,2,4],"boolean":true,"null":null,"number":123,"object":{"a":"b","c":"d","e":"f"},"string":"Hello World","absent-in-right":true,"different-types":123}""").asInstanceOf[JsObject]
val right = Json.parse("""{"array":[1,2,3],"boolean":true,"null":null,"number":456,"object":{"a":"b","c":"d","e":"ff"},"string":"India","absent-in-left":true,"different-types":"YES"}""").asInstanceOf[JsObject]

// '*' - for the root node
showJsDiff(left, right, "*", Seq.empty[String])

def showJsDiff(left: JsValue, right: JsValue, parent: String, path: Seq[String]): Unit = {
  val newPath = path :+ parent
  if (left.getClass != right.getClass) {
    println(s"Cannot compare ${left.getClass.getSimpleName} and ${right.getClass.getSimpleName} " +
      s"in '${getPath(newPath)}'")
  }
  else {
    left match {
      // Primitive types are pretty easy to handle
      case JsNull => logIfNotEqual(JsNull, right.asInstanceOf[JsNull.type], newPath)
      case JsBoolean(value) => logIfNotEqual(value, right.asInstanceOf[JsBoolean].value, newPath)
      case JsNumber(value) => logIfNotEqual(value, right.asInstanceOf[JsNumber].value, newPath)
      case JsString(value) => logIfNotEqual(value, right.asInstanceOf[JsString].value, newPath)
      case JsArray(value) =>
        // For array we have to call showJsDiff on each element of array
        val arr1 = value
        val arr2 = right.asInstanceOf[JsArray].value
        if (arr1.length != arr2.length) {
          println(s"Arrays in '${getPath(newPath)}' have different length. ${arr1.length} != ${arr2.length}")
        }
        else {
          arr1.indices.foreach { idx =>
            showJsDiff(arr1(idx), arr2(idx), s"($idx)", newPath)
          }
        }
      case JsObject(value) =>
        val leftFields = value.keys.toSeq
        val rightJsObject = right.asInstanceOf[JsObject]
        val rightFields = rightJsObject.fields.map { case (name, value) => name }

        val absentInLeft = rightFields.diff(leftFields)
        if (absentInLeft.nonEmpty) {
          println("Next fields are absent in LEFT: ")
          absentInLeft.foreach { fieldName =>
            println(s"\t ${getPath(newPath :+ fieldName)}")
          }
        }
        val absentInRight = leftFields.diff(rightFields)
        if (absentInRight.nonEmpty) {
          println("Next fields are absent in RIGHT: ")
          absentInRight.foreach { fieldName =>
            println(s"\t ${getPath(newPath :+ fieldName)}")
          }
        }
        // For common fields we have to call showJsDiff on them
        val commonFields = leftFields.intersect(rightFields)
        commonFields.foreach { field =>
          showJsDiff(value(field), rightJsObject(field), field, newPath)
        }

    }
  }
}


def logIfNotEqual[T](left: T, right: T, path: Seq[String]): Unit = {
  if (left != right) {
    println(s"'${getPath(path)}' => $left != $right")
  }
}

def getPath(path: Seq[String]): String = path.mkString("\\")

使用 diffson - RFC-6901 和 RFC-6902 的 Scala 實現: https://github.com/gnieh/diffson

json4s has a handy diff function described here: https://github.com/json4s/json4s (search for Merging & Diffing) and API doc here: https://static.javadoc.io/org.json4s/json4s-core_2. 9.1/3.0.0/org/json4s/Diff.html

這是 Artavazd 答案的略微修改版本(順便說一句,非常感謝。)。 此版本將差異輸出到方便的 object 中,而不是僅記錄它們

import play.api.Logger
import play.api.libs.json.{JsArray, JsBoolean, JsError, JsNull, JsNumber, JsObject, JsString, JsSuccess, JsValue, Json, OFormat, Reads}

case class JsDifferences(
  differences: List[JsDifference] = List()
)

object JsDifferences {
  implicit val format: OFormat[JsDifferences] = Json.format[JsDifferences]
}

case class JsDifference(
  key: String,
  path: Seq[String],
  oldValue: Option[String],
  newValue: Option[String]
)

object JsDifference {
  implicit val format: OFormat[JsDifference] = Json.format[JsDifference]
}

object JsonUtils {
  val logger: Logger = Logger(this.getClass)

  def findDiff(left: JsValue, right: JsValue, parent: String = "*", path: List[String] = List()): JsDifferences = {
    val newPath = path :+ parent
    if (left.getClass != right.getClass) {
      logger.debug(s"Cannot compare ${left.getClass.getSimpleName} and ${right.getClass.getSimpleName} in '${getPath(newPath)}'")
      JsDifferences()
    } else left match {
      case JsNull => logIfNotEqual(JsNull, right.asInstanceOf[JsNull.type], newPath)
      case JsBoolean(value) => logIfNotEqual(value, right.asInstanceOf[JsBoolean].value, newPath)
      case JsNumber(value) => logIfNotEqual(value, right.asInstanceOf[JsNumber].value, newPath)
      case JsString(value) => logIfNotEqual(value, right.asInstanceOf[JsString].value, newPath)
      case JsArray(value) =>
        val arr1 = value
        val arr2 = right.asInstanceOf[JsArray].value
        if (arr1.length != arr2.length) {
          logger.debug(s"Arrays in '${getPath(newPath)}' have different length. ${arr1.length} != ${arr2.length}")
          JsDifferences()
        } else JsDifferences(arr1.indices.flatMap(idx => findDiff(arr1(idx), arr2(idx), s"($idx)", newPath).differences).toList)
      case leftJsObject: JsObject => {
        val leftFields = leftJsObject.keys.toSeq
        val rightJsObject = right.asInstanceOf[JsObject]
        val rightFields = rightJsObject.fields.map { case (name, value) => name }

        val keysAbsentInLeft = rightFields.diff(leftFields)
        val leftDifferences = keysAbsentInLeft.map(fieldName => JsDifference(
          key = fieldName, path = newPath :+ fieldName, oldValue = None, newValue = Some(rightJsObject(fieldName).toString)
        ))

        val keysAbsentInRight = leftFields.diff(rightFields)
        val rightDifferences = keysAbsentInRight.map(fieldName => JsDifference(
          key = fieldName, path = newPath :+ fieldName, oldValue = Some(leftJsObject(fieldName).toString), newValue = None
        ))

        val commonKeys = leftFields.intersect(rightFields)
        val commonDifferences = commonKeys.flatMap(field => findDiff(leftJsObject(field), rightJsObject(field), field, newPath).differences).toList

        JsDifferences((leftDifferences ++ rightDifferences ++ commonDifferences).toList)
      }
    }
  }

  def logIfNotEqual[T](left: T, right: T, path: Seq[String]): JsDifferences = {
    if (left != right) {
      JsDifferences(List(JsDifference(
        key = path.last, path = path, oldValue = Some(left.toString), newValue = Some(right.toString)
      )))
    } else JsDifferences()
  }

  def getPath(path: Seq[String]): String = path.mkString("\\")
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM