Created
July 8, 2017 18:39
-
-
Save tvnpraveen/feed137ac1cbfb1482441e0e61e7466e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
xquery version "1.0-ml"; | |
module namespace json = "http://lib/utils/compare-json"; | |
declare variable $json-node-1 := (); | |
declare variable $json-node-2 := (); | |
declare variable $diff-map := map:map(); | |
(:~ This is a recursive function to compare and update the differences to the map | |
:) | |
declare private function json:compare($node as node()){ | |
if(fn:count($node) gt 1) then | |
for $select-node in $node | |
return json:compare($select-node) | |
else if($node instance of object-node() or | |
$node instance of array-node()) then | |
for $child-node in $node/node() | |
return json:compare($child-node) | |
else if($node instance of number-node() or | |
$node instance of boolean-node() or | |
$node instance of null-node() or | |
$node instance of text()) then | |
let $node-path := xdmp:path($node) | |
let $dest-node := xdmp:value(fn:concat("$json-node-2",$node-path)) | |
return | |
if($dest-node) then | |
if($node ne $dest-node) then | |
map:put($diff-map, $node-path, "Source:"||$node||" and Destination:"||$dest-node) | |
else() | |
else(map:put($diff-map, $node-path, "Does not exist in Destination")) | |
else () | |
}; | |
(:~ This function will compare source and destination JSON data and will return a sequence of strings with each difference. | |
: @param $sourceNode - Source JSON String or Node | |
: @param $destNode - Destination JSON String or Node | |
: @return - Sequence of strings with differences | |
: - Return format | |
: <Node Xpath> => Source:<value> and Destination:<value> | |
: | |
:) | |
declare function json:compare-json($sourceNode, $destNode) as xs:string* { | |
let $_ := xdmp:set($json-node-1, if($sourceNode instance of xs:string) then xdmp:unquote($sourceNode) else $sourceNode) | |
let $_ := xdmp:set($json-node-2, if($destNode instance of xs:string) then xdmp:unquote($destNode) else $destNode) | |
let $_ := json:compare($json-node-1/node()) | |
for $key in map:keys($diff-map) | |
return | |
$key||" => "||map:get($diff-map, $key) | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment