首页 > 解决方案 > 使用 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'

标签: pythonjson

解决方案


您只需要从文件中访问正确的元素:

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

推荐阅读