2017-12-06 2 views
2

두 개의 json 파일이 있습니다. 하나는 json이고 다른 하나는 GET API 호출의 결과입니다. 비교하고 파일의 불일치를 찾아야합니다.두 JSON 파일 간의 차이점/불일치를 찾는 방법은 무엇입니까?

예상 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에서 구현해야합니다.

답변

0

예를 들어 play-json library을 사용할 수 있으며 재귀 적으로 두 JSON을 통과 할 수 있습니다. 다음 입력 (당신의 입력보다 더 정교한 비트)의 경우 :

LEFT :

{ 
    "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 
} 

RIGHT :

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' 

코드 :

그것은이 출력을 생성

{ 
    "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" 
} 

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("\\") 
,451,515,
관련 문제