使用Python在JSON文件中的数组中添加键

时间:2019-07-11 08:58:24

标签: 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'

1 个答案:

答案 0 :(得分:1)

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

object(WP_Term)#9457 (17) { ["term_id"]=> int(196) ["name"]=> string(7) "Jan Hus" ["slug"]=> string(7) "jan-hus" ["term_group"]=> int(0) ["term_taxonomy_id"]=> int(196) ["taxonomy"]=> string(8) "category" ["description"]=> string(0) "" ["parent"]=> int(0) ["count"]=> int(11) ["filter"]=> string(3) "raw" ["term_order"]=> string(1) "0" ["cat_ID"]=> int(196) ["category_count"]=> int(11) ["category_description"]=> string(0) "" ["cat_name"]=> string(7) "Jan Hus" ["category_nicename"]=> string(7) "jan-hus" ["category_parent"]=> int(0) }