使用Python更改JSON FILe上的多个键

时间:2019-07-23 08:32:46

标签: python json

如果满足条件,我将尝试将新密钥更新到我的JSON文件中。以下是我的python代码,试图在JSON文件中进行多次更新。

 #!/usr/bin/env python
 # Usage: update json file 
import json
import os

json_dir="/opt/rdm/adggeth/ADGG-ETH-02/20181008/"
json_dir_processed="/opt/rdm/adggeth/ADGG-ETH-02/20181008updated/"
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)
            # replacement mapping
            update_map = {"grp_farmerreg/farmerdetails/farmermobile":"grp_farmerdts/hh_id",
                          "grp_farmerdts/hh_region":"grp_farmerdts/region",
                          "grp_farmerdts/hh_district":"grp_farmerdts/district",
                          "grp_farmerdts/hh_ward":"grp_farmerdts/ward",
                          "grp_farmerdts/hh_village":"grp_farmerdts/village"}

            diff_keys = update_map.keys() - json_data.keys()
            if not diff_keys:
                print("No Update to JSON keys")
            else:
                for k in diff_keys:
                    json_data[k] = json_data[update_map[k]]
        with open(processed_json, 'w') as f:
            f.write(json.dumps(json_data, indent=4))
    else:
        print "%s not a JSON file" % json_file

我要更新的JSON文件如下:

{
    ....
    "farmerregistrd": "1", 
    "grp_farmerdts/region": "5", 
    "datacollid": "0923678275", 
    "_status": "submitted_via_web", 
    "enumtype": "2", 
    "deviceid": "352948096845916", 
    "start_time": "2019-04-03T10:57:23.620+03", 
    "_uuid": "f1069eae-33f8-4850-a549-49fcde27f077", 
    "grp_farmerdts/village": "2852", 
    "_submitted_by": null, 
    "formhub/uuid": "42cb3fc351a74fd89702078160f849ca", 
    "grp_farmerdts/hh_id": "623", 
    "grp_farmerdts/ward": "136", 
    ...
    "_userform_id": "adggeth_ADGG-ETH-REG02-20181008", 
    "_id": 711097, 
    "grp_farmerdts/district": "31"
} 

运行以下python文件的预期输出如下

 {
        ....
        "farmerregistrd": "1", 
        "grp_farmerdts/hh_region": "5", 
        "datacollid": "0923678275", 
        "_status": "submitted_via_web", 
        "enumtype": "2", 
        "deviceid": "352948096845916", 
        "start_time": "2019-04-03T10:57:23.620+03", 
        "_uuid": "f1069eae-33f8-4850-a549-49fcde27f077", 
        "grp_farmerdts/hh_village": "2852", 
        "_submitted_by": null, 
        "formhub/uuid": "42cb3fc351a74fd89702078160f849ca", 
        "grp_farmerdts/hh_id": "623", 
        "grp_farmerdts/hh_ward": "136", 
        ...
        "_userform_id": "adggeth_ADGG-ETH-REG02-20181008", 
        "_id": 711097, 
        "grp_farmerdts/hh_district": "31"
    } 

2 个答案:

答案 0 :(得分:2)

使用re模块和带有json.loads()参数(doc)的object_hook=。该脚本会将hh_前缀添加到不在以下位置的每个grp_farmerdts/*键:

json_str = '''{
    "farmerregistrd": "1",
    "grp_farmerdts/region": "5",
    "datacollid": "0923678275",
    "_status": "submitted_via_web",
    "enumtype": "2",
    "deviceid": "352948096845916",
    "start_time": "2019-04-03T10:57:23.620+03",
    "_uuid": "f1069eae-33f8-4850-a549-49fcde27f077",
    "grp_farmerdts/village": "2852",
    "_submitted_by": null,
    "formhub/uuid": "42cb3fc351a74fd89702078160f849ca",
    "grp_farmerdts/hh_id": "623",
    "grp_farmerdts/ward": "136",
    "_userform_id": "adggeth_ADGG-ETH-REG02-20181008",
    "_id": 711097,
    "grp_farmerdts/district": "31"
}'''

import re
import json

def change_keys(d):
    return {re.sub(r'grp_farmerdts/((?!hh_)(\w+))', r'grp_farmerdts/hh_\1', k): v for k, v in d.items()}

print(json.dumps(json.loads(json_str, object_hook=change_keys), indent=4))

打印:

{
    "farmerregistrd": "1",
    "grp_farmerdts/hh_region": "5",
    "datacollid": "0923678275",
    "_status": "submitted_via_web",
    "enumtype": "2",
    "deviceid": "352948096845916",
    "start_time": "2019-04-03T10:57:23.620+03",
    "_uuid": "f1069eae-33f8-4850-a549-49fcde27f077",
    "grp_farmerdts/hh_village": "2852",
    "_submitted_by": null,
    "formhub/uuid": "42cb3fc351a74fd89702078160f849ca",
    "grp_farmerdts/hh_id": "623",
    "grp_farmerdts/hh_ward": "136",
    "_userform_id": "adggeth_ADGG-ETH-REG02-20181008",
    "_id": 711097,
    "grp_farmerdts/hh_district": "31"
}

答案 1 :(得分:1)

根据您的预期输出,需要检查所有特定键(不是其中之一)。更改逻辑,如下所示:

...
json_data = json.load(f)
# replacement mapping
update_map = {"grp_farmerreg/farmerdetails/farmermobile":"grp_farmerdts/hh_id",
              "grp_farmerdts/hh_region":"grp_farmerdts/region",
              "grp_farmerdts/hh_district":"grp_farmerdts/district",
              "grp_farmerdts/hh_ward":"grp_farmerdts/ward", "grp_farmerdts/hh_village":"grp_farmerdts/village"}

diff_keys = update_map.keys() - json_data.keys()
if not diff_keys:
    print("No Update to JSON keys")
else:
    for k in diff_keys:
        if update_map[k] in json_data:
            json_data[k] = json_data[update_map[k]]