python2 json大数据_python中的多级JSON差异

Please link me to answer if this has already been answered,

my problem is i want to get diff of multilevel json which is unordered.

x=json.loads('''[{"y":2,"x":1},{"x":3,"y":4}]''')

y=json.loads('''[{"x":1,"y":2},{"x":3,"y":4}]''')

z=json.loads('''[{"x":3,"y":4},{"x":1,"y":2}]''')

import json_tools as jt

import json_delta as jd

print jt.diff(y,z)

print jd.diff(y,z)

print y==z

print x==y

output is

[{'prev': 2, 'value': 4, 'replace': u'/0/y'}, {'prev': 1, 'value': 3, 'replace': u'/0/x'}, {'prev': 4, 'value': 2, 'replace': u'/1/y'}, {'prev': 3, 'value': 1, 'replace': u'/1/x'}]

[[[2], {u'y': 2, u'x': 1}], [[0]]]

False

True

my question is how can i get y and z to be equal or if there are actual differences depending on non-order of the JSON.

kind of unordered List of dictionaries but i am looking for something which is level-proof that is list/dict of dictionaries of list/dictionaries ...

解决方案

solved it partially with following function

def diff(prev,lat):

p=prev

l=lat

prevDiff = []

latDiff = []

for d1 in p[:]:

flag = False

for d2 in l:

if len(set(d1.items()) ^ set(d2.items())) == 0:

p.remove(d1)

l.remove(d2)

flag = True

break

if not flag:

prevDiff.append(d1)

p.remove(d1)

prevDiff = prevDiff + p

latDiff = latDiff + l

resJSONdata=[]

if len(prevDiff) != 0:

resJSONdata.append({'prevCount':len(prevDiff)})

resJSONdata.append({'prev':prevDiff})

if len(latDiff) != 0:

resJSONdata.append({'latestCount':len(latDiff)})

resJSONdata.append({'latest':latDiff})

# return json.dumps(resJSONdata,indent = 4,sort_keys=True)

return resJSONdata

it's not doing it recursively into level into levels but for my purpose this solved the issue

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值