Python - 将2个JSON与变量键和值长度

时间:2018-06-01 10:29:55

标签: python json

尝试将2个JSON与可变字段长度进行比较。

到目前为止已经有了这个代码,做了diff风格的JSON比较:

def diffjson(found, expected):
    '''diff two json strings'''
    found = found.split()
    expected = expected.split()
    return difflist(
        found,
        expected,
        fromfile="found",
        tofile="expected",
        rstrip=True)

def difflist( fromlines, tolines, fromfile=None, tofile=None, quiet=False, addnl=None, rstrip=False ) :
    if rstrip :
        fromlines  = [ x.rstrip() for x in fromlines  if x != '' ]
        tolines    = [ x.rstrip() for x in tolines    if x != '' ]
    diff = difflib.unified_diff(fromlines, tolines, fromfile, tofile, n=5)
    diff = list(diff)
    if len(diff) :
        if not quiet :
            if addnl :
                for x in diff :
                    ret = ret + (x)
                    ret = ret + (addnl)
            else :
                ret = diff
        return ret
    else :
        return True

借用pandokia - https://github.com/spacetelescope/pandokia/blob/master/pandokia/helpers/filecomp.py

输出例如:

\--- found +++ expected @@ -3364,11 +3364,11 @@ group global access"]},
"vjafUGJ9H0": {"260":-["test",+["us-east-1", null, "Not enabled", null, null, 

所以这意味着第一个JSON与另一个JSON的不同之处在于" test"。

我的问题是:

是否有不同的方法将JSON与变量键和值长度进行比较?

比较

{"1iG5NDGVre": {"118": ["test1", "test2", "test3", "tcp", "22", "Red", "0.0.0.0/0"]}}

使用:

{"1iG5NDGVre": {"118": ["test1", "test2", "test3", "tcp", "22", "Red", "Blue", "0.0.0.0/0"]}}

谢谢,

0 个答案:

没有答案