python - 使用 Python 在 JSON 文件中的数组中添加键
问题描述
我正在尝试使用以下 python 脚本更新正在更新到我的数据库中的 JSON 文件。
#!/usr/bin/env python
# Usage: update json file
import json
import os
json_dir="Downloads/ADGGTNZ_SERVERFILES/Test_JSON/"
json_dir_processed="Downloads/ADGGTNZ_SERVERFILES/Test_JSON/updated"
for json_file in os.listdir(json_dir):
if json_file.endswith(".json"):
processed_json = "%s%s" % (json_dir_processed, json_file)
json_file = json_dir + json_file
print "Processing %s -> %s" % (json_file, processed_json)
with open(json_file, 'r') as f:
json_data = json.load(f)
json_data_extract = json_data['grp_cowmonitoring/rpt_animrec'][0]
if "grp_cowmonitoring/rpt_animrec/grp_animrec/cowtagid" not in json_data_extract:
json_data["grp_cowmonitoring/rpt_animrec/grp_animrec/cowtagid"] = json_data["grp_cowmonitoring/rpt_animrec/grp_animrec/damtagid"]
with open(processed_json, 'w') as f:
f.write(json.dumps(json_data, indent=4))
else:
print "%s not a JSON file" % json_file
更新脚本的目的是找出
"grp_cowmonitoring/rpt_animrec/grp_animrec/cowtagid"
我的 JSON 数组中是否缺少;然后我将更新相同的密钥,但名称有所不同"grp_cowmonitoring/rpt_animrec/grp_animrec/damtagid"
原始文件
{
"_notes": [],
....
"grp_cowmonitoring/rpt_animrec": [
{
"grp_cowmonitoring/rpt_animrec/grp_animrec/cowtagid": "TZN000403250467",
...
"grp_cowmonitoring/rpt_animrec/grp_milking/grp_calfreg/rpt_reg_calvedets": [
{
"grp_cowmonitoring/rpt_animrec/grp_milking/grp_calfreg/rpt_reg_calvedets/grp_reg_calvedets/calfsex": "1",
"grp_cowmonitoring/rpt_animrec/grp_milking/grp_calfreg/rpt_reg_calvedets/grp_reg_calvedets/calvtype": "1",
....
}
],
"grp_cowmonitoring/rpt_animrec/anim_weight/weight": "343.0",
...
}
],
"fid": 647935,
"grp_cowmonitoring/grp-milkuse/milkprocess": "0.0",
"start_time": "2018-11-30T08:48:32.278+03",
....
}
预期的 JSON 文件
{
"_notes": [],
....
"grp_cowmonitoring/rpt_animrec": [
{
"grp_cowmonitoring/rpt_animrec/grp_animrec/cowtagid": "TZN000403250467",
...
"grp_cowmonitoring/rpt_animrec/grp_milking/grp_calfreg/rpt_reg_calvedets": [
{
"grp_cowmonitoring/rpt_animrec/grp_milking/grp_calfreg/rpt_reg_calvedets/grp_reg_calvedets/calfsex": "1",
"grp_cowmonitoring/rpt_animrec/grp_milking/grp_calfreg/rpt_reg_calvedets/grp_reg_calvedets/calvtype": "1",
"grp_cowmonitoring/rpt_animrec/grp_animrec/damtagid"
....
}
],
"grp_cowmonitoring/rpt_animrec/anim_weight/weight": "343.0",
...
}
],
"fid": 647935,
"grp_cowmonitoring/grp-milkuse/milkprocess": "0.0",
"start_time": "2018-11-30T08:48:32.278+03",
....
}
如何修改我的 python 脚本以适应 JSON 中的更改
更新原代码后的错误信息
Traceback (most recent call last):
File "/opt/rdm/adggtnz/ADGG-TZA-03/addfidkey2.sh", line 15, in <module>
json_data_extract = json_data['grp_cowmonitoring/rpt_animrec'][0]
KeyError: 'grp_cowmonitoring/rpt_animrec'
解决方案
您只需要从文件中访问正确的元素:
import json
import os
json_dir="Downloads/ADGGTNZ_SERVERFILES/Test_JSON/"
json_dir_processed="Downloads/ADGGTNZ_SERVERFILES/Test_JSON/updated/"
for json_file in os.listdir(json_dir):
if json_file.endswith(".json"):
processed_json = "%s%s" % (json_dir_processed, json_file)
json_file = json_dir + json_file
print "Processing %s -> %s" % (json_file, processed_json)
with open(json_file, 'r') as f:
json_data = json.load(f)
json_data_extract = json_data.get('grp_cowmonitoring/rpt_animrec', [])
for cow in json_data_extract:
if "grp_cowmonitoring/rpt_animrec/grp_animrec/cowtagid" not in cow:
# Skip if cowtagid is not present
continue
calves = cow.get("grp_cowmonitoring/rpt_animrec/grp_milking/grp_calfreg/rpt_reg_calvedets", [])
for calf in calves:
if "grp_cowmonitoring/rpt_animrec/grp_animrec/damtagid" not in calf:
print "Updating ..."
calf["grp_cowmonitoring/rpt_animrec/grp_animrec/damtagid"] = cow["grp_cowmonitoring/rpt_animrec/grp_animrec/cowtagid"]
with open(processed_json, 'w') as f:
f.write(json.dumps(json_data, indent=4))
else:
print "%s not a JSON file" % json_file
推荐阅读
- java - 从 List DTO 动态过滤属性
- javascript - map 不是 react js 中的函数
- c++ - 对动态规划中的两类子问题感到困惑
- c# - 如何将错误消息从服务返回到控制器,然后返回到客户端
- java - Memcached-Java-Client 无法从 Linux 服务器检索数据
- c++ - 我无法使用套接字编程在 C++ 中发送发布请求
- javascript - 位置sticky以使表格行彼此粘连
- windows - 如何通过注册表隐藏/禁用特定的应用程序桌面快捷方式?
- c# - asp.net core 中的配置键是否应该不区分大小写?
- javascript - 如何在 Objection.js 的关系映射中过滤 2 个不同值的相同字段