Package: json-regex-difftool
Found a bug? Need a new feature? Want to help out? Check out theContributor’s guideIntroductionA python module for checking equivalence and difference of two JSONs
with regex supportThis repository will treat two types of JSON necessary for our purpose:Regular JSON where only data is storedModel JSON which will treat all keys and values as regular
expressionsModels will be used for comparing JSON objects to A model for
equivalence with regex matchingAs JSON can be in a non-deterministic order, this is harder
than doing a straight string comparisonThere are four modes of this moduleRegular JSON comparisonJSON to Model comparisonRegular JSON diffJSON to Model Diff1. Regular JSON comparisonThis simply tests whether two files contating json are the same./json_diff.pypath/to/file1.jsonpath/to/file2.jsonReturns True if equal or False otherwise..json_diff.pypath/to/file1.jsonpath/to/json_directory/Returns the filename of the first match if there is one, or False
otherwise.2. JSON to Model comparisonThis mode tests whether a given JSON file matches a model when regular
expressions are evaluated../json_diff.py--modej2mpath/to/pure.jsonpath/to/model.jsonReturns True if the JSON and model match, False otherwise../json_diff.py--modej2mpath/to/pure.jsonpath/to/model_directoryReturns the filename of the first match if there is one, or False
otherwise.3. Regular JSON diffThis mode computes a file diff between two JSON files.The logic flow of this code is loosely based off of code found in
https://github.com/monsur/jsoncompareAssumingold.json:{"accounting":[{"firstName":"John","lastName":"Doe","age":23},{"firstName":"Mary","lastName":"Smith","age":31}],"sales":[{"firstName":"Sally","lastName":"Green","age":27},{"firstName":"Jim","lastName":"Galley","age":41}]}and new.json:{"accounting":[{"firstName":true,"lastName":"Doe","age":23},{"firstName":"Susan","lastName":"Smith","age":31}],"sales":[{"firstName":"Sally","lastName":"Green","size":27},{"firstName":"Jim","age":41}]}ThenShould produce output like:2015-03-2412:59:08,576-jsondiff-INFO-TypeDifference:accounting[0].firstName-isbool:(True),butwasunicode:(John)2015-03-2412:59:08,576-jsondiff-INFO-Changed:accounting[1].firstNametoSusanfromMary2015-03-2412:59:08,576-jsondiff-INFO-+:sales[0].size=272015-03-2412:59:08,576-jsondiff-INFO--:sales[0].age=272015-03-2412:59:08,576-jsondiff-INFO--:sales[1].lastName=Galley4. JSON to Model DiffThis mode computes a file diff between a JSON file and a model with
regular expressions.*Note: at this time, we only support regular expression matching on
singleton values (that is values that are NOT a list or dictionary)*Assumingnew.json:{"accounting":[{"firstName":"John","lastName":"Doe","age":23},{"firstName":"Mary","lastName":"Smith","age":31}],"sales":[{"firstName":"Sally","lastName":"Green","age":27},{"firstName":"Jim","lastName":"Galley","age":41}]}and model.json:{"accounting":[{"lastName":"Doe","age":23},{"firstName":"Mary","lastName":"Smith","age":"[0-9]+"}],"sales":[{"firstName":"Sally","lastName":"(.*)","age":24},{"firstName":"Jim","lastName":"Galley","age":41}]}Then.json_diff.py-d--modej2mnew.jsonmodel.jsonShould produce output2015-03-2412:59:08,576-jsondiff-INFO-+:accounting[0].firstName=John2015-03-2412:59:08,576-jsondiff-INFO-Changed:sales[0].ageto27from24As you can see we find a match on numbers even though the type is ‘int’
(Mary Smith’s age). Also, even though we changed Sally’s age, we were
still able to find a match on her entry with a regular expression
representing her last name.