将基于另一个 JSON 的 JSON 值替换为相同的列
Replace JSON values based on another JSON with same columns
我是处理 JSON 文件的新手。我有两个 JSON 文件,如下所示:
J1:
{'country': PA,
'year': '2021',
'month': '10',
'client': 'x',
'data': [{'id': 'g0084',
'lat': y,
'lon': q,
'avg_audience': 87.84,
'audiences': [{'day': '01', 'audience': 84},
{'day': '02', 'audience': 83},
{'day': '03', 'audience': 52},
...
{'id': 'g0093',
'lat': y,
'lon': q,
'avg_audience': 36.13,
'audiences': [{'day': '01', 'audience': 48},
{'day': '02', 'audience': 47},
{'day': '03', 'audience': 39},
J2:
[{'id': 'g0084', 'day': '01', 'audience': 190365.76367661008},
{'id': 'g0084', 'day': '02', 'audience': 186712.00627779233},
{'id': 'g0084', 'day': '03', 'audience': 161048.6891385768},
...
{'id': 'g0093', 'day': '01', 'audience': 108780.43638663433},
{'id': 'g0093', 'day': '02', 'audience': 105728.4854826053},
{'id': 'g0093', 'day': '03', 'audience': 120786.51685393258},
所以我想用 J2 中的受众值替换 J1 中的受众值。基于相同的 ID 和日期。我想要的输出如下所示:
{'country': PA,
'year': '2021',
'month': '10',
'client': 'x',
'data': [{'id': 'g0084',
'lat': y,
'lon': q,
'avg_audience': 87.84,
'audiences': [{'day': '01', 'audience': 190365.76367661008}},
{'day': '02', 'audience': 186712.00627779233},
{'day': '03', 'audience': 161048.6891385768},
...
{'id': 'g0093',
'lat': y,
'lon': q,
'avg_audience': 36.13,
'audiences': [{'day': '01', 'audience': 108780.4},
{'day': '02', 'audience': 105728.48},
{'day': '03', 'audience': 105728.48},
而且我不知道该怎么做。有什么帮助吗?
这里有一个方法可以做到这一点,但我想先吐槽一下。似乎越来越多的人正在使用 JSON 来正确存储在数据库中的内容。这是一个这样的例子。 JSON 是为数据交换而设计的,而不是为 long-term 存储而设计的。
但是即使不使用数据库,这里的数据结构也不正确。为什么 'data'
是对象列表,而不是 'id'
是键的对象?与 audiences
相同。那不应该是一个记录列表,它应该是一个对象,其中 'day'
是键,'audience'
是值。这些更改将使此更新变得微不足道。实际上,如果您有数千条记录,我可能会在开始更新之前创建 J1 的字典索引。
J1 = {'country': 'PA',
'year': '2021',
'month': '10',
'client': 'x',
'data': [
{'id': 'g0084',
'lat': 123.456,
'lon': 123.456,
'avg_audience': 87.84,
'audiences': [{'day': '01', 'audience': 84},
{'day': '02', 'audience': 83},
{'day': '03', 'audience': 52},
]
},
{'id': 'g0093',
'lat': 123.456,
'lon': 123.456,
'avg_audience': 36.13,
'audiences': [{'day': '01', 'audience': 48},
{'day': '02', 'audience': 47},
{'day': '03', 'audience': 39}
]
}
]
}
J2 = [
{'id': 'g0084', 'day': '01', 'audience': 190365.76367661008},
{'id': 'g0084', 'day': '02', 'audience': 186712.00627779233},
{'id': 'g0084', 'day': '03', 'audience': 161048.6891385768},
{'id': 'g0093', 'day': '01', 'audience': 108780.43638663433},
{'id': 'g0093', 'day': '02', 'audience': 105728.4854826053},
{'id': 'g0093', 'day': '03', 'audience': 120786.51685393258}
]
# Make an index into J1['data].
idx = {}
for row in J1['data']:
idx[row['id']] = row
for row in J2:
# Find the id.
idx = [t for t in J1['data'] if t['id'] == row['id']][0]
# Find the day.
day = [t for t in idx['audiences'] if t['day'] == row['day']][0]
day['audience'] = row['audience']
from pprint import pprint
pprint(J1)
输出:
{'client': 'x',
'country': 'PA',
'data': [{'audiences': [{'audience': 190365.76367661008, 'day': '01'},
{'audience': 186712.00627779233, 'day': '02'},
{'audience': 161048.6891385768, 'day': '03'}],
'avg_audience': 87.84,
'id': 'g0084',
'lat': 123.456,
'lon': 123.456},
{'audiences': [{'audience': 108780.43638663433, 'day': '01'},
{'audience': 105728.4854826053, 'day': '02'},
{'audience': 120786.51685393258, 'day': '03'}],
'avg_audience': 36.13,
'id': 'g0093',
'lat': 123.456,
'lon': 123.456}],
'month': '10',
'year': '2021'}
我是处理 JSON 文件的新手。我有两个 JSON 文件,如下所示:
J1:
{'country': PA,
'year': '2021',
'month': '10',
'client': 'x',
'data': [{'id': 'g0084',
'lat': y,
'lon': q,
'avg_audience': 87.84,
'audiences': [{'day': '01', 'audience': 84},
{'day': '02', 'audience': 83},
{'day': '03', 'audience': 52},
...
{'id': 'g0093',
'lat': y,
'lon': q,
'avg_audience': 36.13,
'audiences': [{'day': '01', 'audience': 48},
{'day': '02', 'audience': 47},
{'day': '03', 'audience': 39},
J2:
[{'id': 'g0084', 'day': '01', 'audience': 190365.76367661008},
{'id': 'g0084', 'day': '02', 'audience': 186712.00627779233},
{'id': 'g0084', 'day': '03', 'audience': 161048.6891385768},
...
{'id': 'g0093', 'day': '01', 'audience': 108780.43638663433},
{'id': 'g0093', 'day': '02', 'audience': 105728.4854826053},
{'id': 'g0093', 'day': '03', 'audience': 120786.51685393258},
所以我想用 J2 中的受众值替换 J1 中的受众值。基于相同的 ID 和日期。我想要的输出如下所示:
{'country': PA,
'year': '2021',
'month': '10',
'client': 'x',
'data': [{'id': 'g0084',
'lat': y,
'lon': q,
'avg_audience': 87.84,
'audiences': [{'day': '01', 'audience': 190365.76367661008}},
{'day': '02', 'audience': 186712.00627779233},
{'day': '03', 'audience': 161048.6891385768},
...
{'id': 'g0093',
'lat': y,
'lon': q,
'avg_audience': 36.13,
'audiences': [{'day': '01', 'audience': 108780.4},
{'day': '02', 'audience': 105728.48},
{'day': '03', 'audience': 105728.48},
而且我不知道该怎么做。有什么帮助吗?
这里有一个方法可以做到这一点,但我想先吐槽一下。似乎越来越多的人正在使用 JSON 来正确存储在数据库中的内容。这是一个这样的例子。 JSON 是为数据交换而设计的,而不是为 long-term 存储而设计的。
但是即使不使用数据库,这里的数据结构也不正确。为什么 'data'
是对象列表,而不是 'id'
是键的对象?与 audiences
相同。那不应该是一个记录列表,它应该是一个对象,其中 'day'
是键,'audience'
是值。这些更改将使此更新变得微不足道。实际上,如果您有数千条记录,我可能会在开始更新之前创建 J1 的字典索引。
J1 = {'country': 'PA',
'year': '2021',
'month': '10',
'client': 'x',
'data': [
{'id': 'g0084',
'lat': 123.456,
'lon': 123.456,
'avg_audience': 87.84,
'audiences': [{'day': '01', 'audience': 84},
{'day': '02', 'audience': 83},
{'day': '03', 'audience': 52},
]
},
{'id': 'g0093',
'lat': 123.456,
'lon': 123.456,
'avg_audience': 36.13,
'audiences': [{'day': '01', 'audience': 48},
{'day': '02', 'audience': 47},
{'day': '03', 'audience': 39}
]
}
]
}
J2 = [
{'id': 'g0084', 'day': '01', 'audience': 190365.76367661008},
{'id': 'g0084', 'day': '02', 'audience': 186712.00627779233},
{'id': 'g0084', 'day': '03', 'audience': 161048.6891385768},
{'id': 'g0093', 'day': '01', 'audience': 108780.43638663433},
{'id': 'g0093', 'day': '02', 'audience': 105728.4854826053},
{'id': 'g0093', 'day': '03', 'audience': 120786.51685393258}
]
# Make an index into J1['data].
idx = {}
for row in J1['data']:
idx[row['id']] = row
for row in J2:
# Find the id.
idx = [t for t in J1['data'] if t['id'] == row['id']][0]
# Find the day.
day = [t for t in idx['audiences'] if t['day'] == row['day']][0]
day['audience'] = row['audience']
from pprint import pprint
pprint(J1)
输出:
{'client': 'x',
'country': 'PA',
'data': [{'audiences': [{'audience': 190365.76367661008, 'day': '01'},
{'audience': 186712.00627779233, 'day': '02'},
{'audience': 161048.6891385768, 'day': '03'}],
'avg_audience': 87.84,
'id': 'g0084',
'lat': 123.456,
'lon': 123.456},
{'audiences': [{'audience': 108780.43638663433, 'day': '01'},
{'audience': 105728.4854826053, 'day': '02'},
{'audience': 120786.51685393258, 'day': '03'}],
'avg_audience': 36.13,
'id': 'g0093',
'lat': 123.456,
'lon': 123.456}],
'month': '10',
'year': '2021'}