python - 在 json 中搜索键,如果该数组中不存在键,则添加默认值

标签 python json python-3.x list dictionary

我有一个 json 数据,例如:

{'data': [{'shares': {'count': 4}, 
   'id':'226196778181357_353462548788112'},
   {'shares': {'count': 5}, 'id': '226196778181357_353075852160115'},
   {'shares': {'count': 47}, 'id': '226196778181357_350624229071944'},
   {'shares': {'count': 3}, 'id': '226196778181357_350028335798200'},
   {'shares': {'count': 5}, 'id': '226196778181357_349395549194812'},
   {'shares': {'count': 6}, 'id': '226196778181357_348637945937239'},
   {'shares': {'count': 3}, 'id': '226196778181357_342294163238284'},
   {'id': '226196778181357_341686736632360'},
   {'shares': {'count': 1}, 'id': '226196778181357_341024573365243'},
   {'shares': {'count': 9}, 'id': '226196778181357_339828576818176'},
   {'shares': {'count': 5}, 'id': '226196778181357_338017080332659'},
   {'shares': {'count': 10}, 'id': '226196778181357_337493103718390'},
   {'shares': {'count': 1}, 'id': '226196778181357_337432203724480'},
   {'shares': {'count': 12}, 'id': '226196778181357_336955527105481'},
   {'shares': {'count': 7}, 'id': '226196778181357_335994360534931'},
   {'shares': {'count': 11}, 'id': '226196778181357_335979820536385'},
   {'shares': {'count': 7}, 'id': '226196778181357_333787794088921'},
   {'shares': {'count': 5}, 'id': '226196778181357_333174720816895'},
   {'shares': {'count': 14}, 'id': '226196778181357_332554220878945'}]}

我想在数据中搜索 dict key ={'shares'} (如果不存在),然后添加默认字典,即 'shares': {'count': 0}

最终输出如下:

{'data': [{'shares': {'count': 4}, 
'id':'226196778181357_353462548788112'},
{'shares': {'count': 5}, 'id': '226196778181357_353075852160115'},
{'shares': {'count': 47}, 'id': '226196778181357_350624229071944'},
{'shares': {'count': 3}, 'id': '226196778181357_350028335798200'},
{'shares': {'count': 5}, 'id': '226196778181357_349395549194812'},
{'shares': {'count': 6}, 'id': '226196778181357_348637945937239'},
{'shares': {'count': 3}, 'id': '226196778181357_342294163238284'},
{'shares': {'count': 0},'id': '226196778181357_341686736632360'},
{'shares': {'count': 1}, 'id': '226196778181357_341024573365243'},
{'shares': {'count': 9}, 'id': '226196778181357_339828576818176'},
{'shares': {'count': 5}, 'id': '226196778181357_338017080332659'},
{'shares': {'count': 10}, 'id': '226196778181357_337493103718390'},
{'shares': {'count': 1}, 'id': '226196778181357_337432203724480'},
{'shares': {'count': 12}, 'id': '226196778181357_336955527105481'},
{'shares': {'count': 7}, 'id': '226196778181357_335994360534931'},
{'shares': {'count': 11}, 'id': '226196778181357_335979820536385'},
{'shares': {'count': 7}, 'id': '226196778181357_333787794088921'},
{'shares': {'count': 5}, 'id': '226196778181357_333174720816895'},
{'shares': {'count': 14}, 'id': '226196778181357_332554220878945'}]}

最佳答案

试试这个:

for e in j['data']:
    if not 'shares' in e:
        e['shares'] = {'count': 0}

jjson.loads(your_json)

关于python - 在 json 中搜索键,如果该数组中不存在键,则添加默认值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53408235/

相关文章:

python - 如何使用 python pandas 分组并计算新字段?

python - 返回第一个元素 python

c# - 将 WP8 升级到 Silverlight WP8.1,有效负载包含两个或更多具有相同目标的文件

arrays - Perl Dancer - JSON 序列化器和 JSON 数组

python - 如何在抓取之前从数据库获取 url 和行 id 以在管道中使用它来存储数据?

python - 使用具有完全访问权限的 Boto3 将文件上传到 AWS S3

php - 通过android编辑mysql

python-3.x - 一个进程请求访问一个对象,但没有被授予这些访问权限在 windows 10 上使用 Selenuim 和 Chrome 驱动程序

python - 如何在Python中从另一个数组中随机填充一个数组?

python - 值错误 : No module named 'notmigrations' during unit tests