Package: diffjson
diffjsonUtilities to diff json data.https://nfwprod.github.io/diffjson/FeaturesEnable search like XPATH.Enable diff for multi json data.Branch Class for SearchBranch ClassConvert dict/list json data to Branch class format by 'generate_branch'.
Branch class is hierachical class tree.RootBranchRoot for all child branches.Provide search and other methods for users.DictBranchChild branch for dict format child.ListBranchChild branch for list format child.LeafEdge branch for str, bool, int, and float.BranchBranch common class for inheritate.SearchBranch class accept search with xpath like strings. (Strongly inspired by josonpath-ng!!)# Example Data, sample.yamlbranch01:b01-01:stringb01-02:1b01-03:2.0b01-04:Truebranch02:b02-01:-name:n02-01-i01value:v02-01-i01-name:n02-01-i02value:b02-01-i02-01:-name:n02-01-i02-01-i01value:v02-01-i02-01-i01-name:n02-01-i02-01-i02value:v02-01-i02-01-i02-name:n02-01-i02-01-i03value:v02-01-i02-01-i03b02-01-i02-02:name:n02-01-i02-02value:v02-01-i02-02-name:n02-01-i03value:b02-01-i03-01:v02-01-i03-01branch03:b03-01:nullimportdiffjsonimportyamlwithopen('sample.yaml'),'r')asf:sampledata=yaml.safe_load(f)# Get dict format data under b01-01b=diffjson.generate_branch(sampledata)result=b.search('/branch01/b01-01')print(result)>['string']Search returns all matched data as List style.DiffBranch Class for Diff JSON DataDiffBranch ClassDiff multi json data as Branch instance by 'diff_branches'.DiffRootBranchRoot for all child diff branches.DiffCommonBranchChild branch for all data format.Diff for dict, list and leaf are contained in DiffCommonBranch.Branch.DiffBranchBranch common class for inheritate.Diffdiffbranch=diffjson.diff_branch([data01,data02,data03])# Export diff in csv formatdiffbranch.export_csv('./diff.csv')NodenameMasks OptionsSometimes data are contained in list format and orders are changes randomly.
For example,,Data Before.-name:id01value:data01-name:id02value:data02-name:id03value:data03Data After.-name:id01value:data01-name:id03value:data03-name:id02value:changedWe want to diff "name: id02" and "name: id02".
Don't want to diff second data "name: id02" and "name: id03".For such case, use mask function.masks={'/':lambdax:x['name'],'/branch01':lambdax:x['id']}diffbranch=diffjson.diff_branch([data01,data02,data03],nodenamemasks=masks)Nodename masks convert list part to dict part with lambda generated key like follows.Data Beforeid01:name:id01value:data01id02:name:id02value:data02id03:name:id03value:data03Data Afterid01:name:id01value:data01id02:name:id02value:changedid03:name:id03value:data03Diff SearchDiffBranch class accept search.Arguments are as follows.locationpath_string(str): XPath format search string.details(bool, option): Return searched path with value, default: False(value only).dump_mode(str, option): DiffTwoBranch only.'all': Dump all branch.'added': Dump added branch. Include added root and children.'bulk_added': Dump added root branch, ignore children of them.'removed': Dump removed branch. Include added root and children.'bulk_removed': Dump removed root branch, ignore children of them.'changed': Dump changed branch only.diffbranch=diffjson.diff_branch([data01,data02,data03])# Searchdiffbranch.search('//',dump_mode='added',details=True)