Python脚本编辑JSON字段

时间:2018-11-01 08:26:08

标签: python json

我想编写一个Python脚本,该脚本循环遍历几个JSON文件并检查是否有如下所示的属性(KEY:VALUE)

"grp_farmerreg/farmerdetails/farmermobile": "0756625759",

存在,如果没有,请检查hh_id,如下所示n

"grp_farmerdts/hh_id": "0753693707",

在确认hh_id的可用性和不存在的farmemobile之后,我想添加

"grp_farmerreg/farmerdetails/farmermobile":(number from hh_id)

这是我的不带农夫移动设备的JSON文件的示例

{
    "_notes": [], 
    "_bamboo_dataset_id": "", 
    "_tags": [], 
    "regdate": "2018-10-17", 
    "gpsloc": "-9.109567398208265 32.947977763771334 1594.4399713018483 8.0", 
    "_xform_id_string": "ADGG-TZA-REG02-20181008", 
    "meta/instanceID": "uuid:4d5372ed-34f0-4ac3-b151-f4f57b746ad4", 
    "_duration": "", 
    "grp_calfreg/rpt_b_calvedets": [
        {
            "grp_calfreg/rpt_b_calvedets/rpt_b_calvedets_layout/calfsex": "2", 
            "grp_calfreg/rpt_b_calvedets/calv_alive_geninfo/calfname": "Nill", 
            "grp_calfreg/rpt_b_calvedets/calc_calfsireid": "TNZ000404011991", 
            "grp_calfreg/rpt_b_calvedets/calfSire_Owner": "3", 
            "grp_calfreg/rpt_b_calvedets/calfsirebrthyrknown": "0", 
            "grp_calfreg/rpt_b_calvedets/rpt_b_calvedets_layout/easecalv": "2", 
            "grp_calfreg/rpt_b_calvedets/calv_alive_geninfo/calftagimage": "1539766349136.jpg", 
            "grp_calfreg/rpt_b_calvedets/calfsiredetails/calfSire_secBreed": "2", 
            "grp_calfreg/rpt_b_calvedets/calv_alive_layout/calvmeasures/calfhgirth": "100.0", 
            "grp_calfreg/rpt_b_calvedets/calfsiredetails/calfSire_ShortName": "Nill", 
            "grp_calfreg/rpt_b_calvedets/calv_alive_geninfo/calftagprefix": "00040401", 
            "grp_calfreg/rpt_b_calvedets/calc_calfid": "TNZ000404014693", 
            "grp_calfreg/rpt_b_calvedets/grp_calfid/Confirm_CalfID": "OK", 
            "grp_calfreg/rpt_b_calvedets/calfSire_OwnerFarmer": "Nill", 
            "grp_calfreg/rpt_b_calvedets/calfSire_TagID": "000404011991", 
            "grp_calfreg/rpt_b_calvedets/calv_alive_layout/calvmeasures/calfweight": "80.0", 
            "grp_calfreg/rpt_b_calvedets/grp_calfid/Calf_ID": "TNZ000404014693", 
            "grp_calfreg/rpt_b_calvedets/feedmthd/feedmth": "-66", 
            "grp_calfreg/rpt_b_calvedets/calv_alive_layout/calvmeasures/calfbodyscore": "4.0", 
            "grp_calfreg/rpt_b_calvedets/calfsirentregstrd": "1", 
            "grp_calfreg/rpt_b_calvedets/calfsirebrthdateknown": "0", 
            "grp_calfreg/rpt_b_calvedets/calfsiredetails/calfSire_mainComp": "5", 
            "grp_calfreg/rpt_b_calvedets/grp_calfsireid/Confirm_CalfsireID": "OK", 
            "grp_calfreg/rpt_b_calvedets/grp_calfsireid/Calfsire_ID": "TNZ000404011991", 
            "grp_calfreg/rpt_b_calvedets/calv_alive_layout/fmlcalfuse/intuse": "1", 
            "grp_calfreg/rpt_b_calvedets/rpt_b_calvedets_layout/calfsiretype": "1", 
            "grp_calfreg/rpt_b_calvedets/rpt_b_calvedets_layout/calvtype": "1", 
            "grp_calfreg/rpt_b_calvedets/rpt_b_calvedets_layout/birthtyp": "1", 
            "grp_calfreg/rpt_b_calvedets/rpt_b_calvedets_layout/calvdatealv": "2018-05-09", 
            "grp_calfreg/rpt_b_calvedets/rpt_b_calvedets_layout/damid": "TZN000404014225", 
            "grp_calfreg/rpt_b_calvedets/calv_alive_geninfo/calftagsec": "4693", 
            "grp_calfreg/rpt_b_calvedets/calfsiredetails/calfSire_Country": "Unknown", 
            "grp_calfreg/rpt_b_calvedets/damidyesno": "1", 
            "grp_calfreg/rpt_b_calvedets/calv_alive_geninfo/calfbodyimage": "1539766363236.jpg", 
            "grp_calfreg/rpt_b_calvedets/calfsiredetails/calfSire_mainBreed": "2", 
            "grp_calfreg/rpt_b_calvedets/calfsiredetails/calfSire_HerdBkRegNo": "Nill", 
            "grp_calfreg/rpt_b_calvedets/feedmthd/feedmthoth": "Supliment", 
            "grp_calfreg/rpt_b_calvedets/calv_alive_layout/calfdef/calfdeformities": "1", 
            "grp_calfreg/rpt_b_calvedets/calfsireregisterd": "0", 
            "grp_calfreg/rpt_b_calvedets/calv_alive_geninfo/calfcolor": "Black and white", 
            "grp_calfreg/rpt_b_calvedets/calv_alive_layout/calfdef/calfwightknown": "1", 
            "grp_calfreg/rpt_b_calvedets/calfsiredetails/calfSire_RegisteredName": "Nill"
        }
    ], 
    "grp_farmerdts/region": "1007", 
    "_geolocation": [
        -9.109567398208265, 
        32.947977763771334
    ], 
    "datacollid": "0758990688", 
    "_status": "submitted_via_web", 
    "farmerregistrd": "1", 
    "deviceid": "353422071089618", 
    "start_time": "2018-10-17T11:45:49.406+03", 
    "_uuid": "4d5372ed-34f0-4ac3-b151-f4f57b746ad4", 
    "grp_farmerdts/village": "4864", 
    "enumtype": "2", 
    "_submitted_by": null, 
    "formhub/uuid": "678f27aca56149a391069c33019a34fc", 
    "grp_farmerdts/hh_id": "0753693707", 
    "_id": 626391, 
    "regactivity": "5", 
    "_submission_time": "2018-10-22T03:18:24", 
    "_version": "20181008", 
    "_attachments": [
        {
            "mimetype": "image/jpeg", 
            "download_url": "http://localhost:8000/media/adggtnz/attachments/1539766349136.jpg", 
            "filename": "adggtnz/attachments/1539766349136.jpg", 
            "instance": 626391, 
            "id": 102154, 
            "xform": 3443
        }, 
        {
            "mimetype": "image/jpeg", 
            "download_url": "http://localhost:8000/media/adggtnz/attachments/1539766363236.jpg", 
            "filename": "adggtnz/attachments/1539766363236.jpg", 
            "instance": 626391, 
            "id": 102153, 
            "xform": 3443
        }
    ], 
    "end_time": "2018-10-17T11:53:41.354+03", 
    "grp_farmerdts/country": "2", 
    "_userform_id": "adggtnz_ADGG-TZA-REG02-20181008", 
    "grp_farmerdts/ward": "1807", 
    "grp_farmerdts/district": "1037"
}

我想将输出结果放在另一个文件夹中,以显示原始和更新的JSON文件

预期输出:

 {
        "_notes": [], 
        "_bamboo_dataset_id": "", 
        "_tags": [], 
        "regdate": "2018-10-17", 
        "gpsloc": "-9.109567398208265 32.947977763771334 1594.4399713018483 8.0", 
        "_xform_id_string": "ADGG-TZA-REG02-20181008", 
        "meta/instanceID": "uuid:4d5372ed-34f0-4ac3-b151-f4f57b746ad4", 
        "_duration": "", 
        "grp_calfreg/rpt_b_calvedets": [
            {
                "grp_calfreg/rpt_b_calvedets/rpt_b_calvedets_layout/calfsex": "2", 
                "grp_calfreg/rpt_b_calvedets/calv_alive_geninfo/calfname": "Nill", 
                "grp_calfreg/rpt_b_calvedets/calc_calfsireid": "TNZ000404011991", 
                "grp_calfreg/rpt_b_calvedets/calfSire_Owner": "3", 
                "grp_calfreg/rpt_b_calvedets/calfsirebrthyrknown": "0", 
                "grp_calfreg/rpt_b_calvedets/rpt_b_calvedets_layout/easecalv": "2", 
                "grp_calfreg/rpt_b_calvedets/calv_alive_geninfo/calftagimage": "1539766349136.jpg", 
                "grp_calfreg/rpt_b_calvedets/calfsiredetails/calfSire_secBreed": "2", 
                "grp_calfreg/rpt_b_calvedets/calv_alive_layout/calvmeasures/calfhgirth": "100.0", 
                "grp_calfreg/rpt_b_calvedets/calfsiredetails/calfSire_ShortName": "Nill", 
                "grp_calfreg/rpt_b_calvedets/calv_alive_geninfo/calftagprefix": "00040401", 
                "grp_calfreg/rpt_b_calvedets/calc_calfid": "TNZ000404014693", 
                "grp_calfreg/rpt_b_calvedets/grp_calfid/Confirm_CalfID": "OK", 
                "grp_calfreg/rpt_b_calvedets/calfSire_OwnerFarmer": "Nill", 
                "grp_calfreg/rpt_b_calvedets/calfSire_TagID": "000404011991", 
                "grp_calfreg/rpt_b_calvedets/calv_alive_layout/calvmeasures/calfweight": "80.0", 
                "grp_calfreg/rpt_b_calvedets/grp_calfid/Calf_ID": "TNZ000404014693", 
                "grp_calfreg/rpt_b_calvedets/feedmthd/feedmth": "-66", 
                "grp_calfreg/rpt_b_calvedets/calv_alive_layout/calvmeasures/calfbodyscore": "4.0", 
                "grp_calfreg/rpt_b_calvedets/calfsirentregstrd": "1", 
                "grp_calfreg/rpt_b_calvedets/calfsirebrthdateknown": "0", 
                "grp_calfreg/rpt_b_calvedets/calfsiredetails/calfSire_mainComp": "5", 
                "grp_calfreg/rpt_b_calvedets/grp_calfsireid/Confirm_CalfsireID": "OK", 
                "grp_calfreg/rpt_b_calvedets/grp_calfsireid/Calfsire_ID": "TNZ000404011991", 
                "grp_calfreg/rpt_b_calvedets/calv_alive_layout/fmlcalfuse/intuse": "1", 
                "grp_calfreg/rpt_b_calvedets/rpt_b_calvedets_layout/calfsiretype": "1", 
                "grp_calfreg/rpt_b_calvedets/rpt_b_calvedets_layout/calvtype": "1", 
                "grp_calfreg/rpt_b_calvedets/rpt_b_calvedets_layout/birthtyp": "1", 
                "grp_calfreg/rpt_b_calvedets/rpt_b_calvedets_layout/calvdatealv": "2018-05-09", 
                "grp_calfreg/rpt_b_calvedets/rpt_b_calvedets_layout/damid": "TZN000404014225", 
                "grp_calfreg/rpt_b_calvedets/calv_alive_geninfo/calftagsec": "4693", 
                "grp_calfreg/rpt_b_calvedets/calfsiredetails/calfSire_Country": "Unknown", 
                "grp_calfreg/rpt_b_calvedets/damidyesno": "1", 
                "grp_calfreg/rpt_b_calvedets/calv_alive_geninfo/calfbodyimage": "1539766363236.jpg", 
                "grp_calfreg/rpt_b_calvedets/calfsiredetails/calfSire_mainBreed": "2", 
                "grp_calfreg/rpt_b_calvedets/calfsiredetails/calfSire_HerdBkRegNo": "Nill", 
                "grp_calfreg/rpt_b_calvedets/feedmthd/feedmthoth": "Supliment", 
                "grp_calfreg/rpt_b_calvedets/calv_alive_layout/calfdef/calfdeformities": "1", 
                "grp_calfreg/rpt_b_calvedets/calfsireregisterd": "0", 
                "grp_calfreg/rpt_b_calvedets/calv_alive_geninfo/calfcolor": "Black and white", 
                "grp_calfreg/rpt_b_calvedets/calv_alive_layout/calfdef/calfwightknown": "1", 
                "grp_calfreg/rpt_b_calvedets/calfsiredetails/calfSire_RegisteredName": "Nill"
            }
        ], 
        "grp_farmerdts/region": "1007", 
        "_geolocation": [
            -9.109567398208265, 
            32.947977763771334
        ], 
        "datacollid": "0758990688", 
        "_status": "submitted_via_web", 
        "farmerregistrd": "1", 
        "deviceid": "353422071089618", 
        "start_time": "2018-10-17T11:45:49.406+03", 
        "_uuid": "4d5372ed-34f0-4ac3-b151-f4f57b746ad4", 
        "grp_farmerdts/village": "4864", 
        "enumtype": "2", 
        "_submitted_by": null, 
        "formhub/uuid": "678f27aca56149a391069c33019a34fc", 
        "grp_farmerdts/hh_id": "0753693707", 
        "grp_farmerreg/farmerdetails/farmermobile": "0753693707",
        "_id": 626391, 
        "regactivity": "5", 
        "_submission_time": "2018-10-22T03:18:24", 
        "_version": "20181008", 
        "_attachments": [
            {
                "mimetype": "image/jpeg", 
                "download_url": "http://localhost:8000/media/adggtnz/attachments/1539766349136.jpg", 
                "filename": "adggtnz/attachments/1539766349136.jpg", 
                "instance": 626391, 
                "id": 102154, 
                "xform": 3443
            }, 
            {
                "mimetype": "image/jpeg", 
                "download_url": "http://localhost:8000/media/adggtnz/attachments/1539766363236.jpg", 
                "filename": "adggtnz/attachments/1539766363236.jpg", 
                "instance": 626391, 
                "id": 102153, 
                "xform": 3443
            }
        ], 
        "end_time": "2018-10-17T11:53:41.354+03", 
        "grp_farmerdts/country": "2", 
        "_userform_id": "adggtnz_ADGG-TZA-REG02-20181008", 
        "grp_farmerdts/ward": "1807", 
        "grp_farmerdts/district": "1037"
    }

这是我尝试过的:

import json
import os

json_dir="/new/20180116/"
json_dir_processed="/new/20180116updated/"
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['"grp_farmerreg/farmerdetails/farmermobile": "0753693707"'] = json_data['"grp_farmerdts/hh_id": "0753693707" ']
        with open(processed_json, 'w') as f:
            f.write(json.dumps(json_data, indent=4))
    else:
        print "%s not a JSON file" % json_file

我的脚本导致以下错误

Traceback (most recent call last):
  File "new.py", line 15, in <module>
    json_data['"grp_farmerreg/farmerdetails/farmermobile": "0753693707"'] = json_data['"grp_farmerdts/hh_id": "0753693707" ']
KeyError: '"grp_farmerdts/hh_id": "0753693707" '

2 个答案:

答案 0 :(得分:1)

这是我根据你们的输入编写的新脚本,它有效

import json
import os

json_dir="/media/dmogaka/CENTOS/newforms/regTZA/20181008/"
json_dir_processed="/media/dmogaka/CENTOS/newforms/regTZA/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)
            if "grp_farmerreg/farmerdetails/farmermobile" not in json_data:                
                json_data["grp_farmerreg/farmerdetails/farmermobile"] = json_data["grp_farmerdts/hh_id"]
        with open(processed_json, 'w') as f:
            f.write(json.dumps(json_data, indent=4))
    else:
        print "%s not a JSON file" % json_file

答案 1 :(得分:0)

我不确定100%确实要做什么,但是我不确定

json_data['"grp_farmerreg/farmerdetails/farmermobile": "0753693707"'] = json_data['"grp_farmerdts/hh_id": "0753693707" ']

似乎是我的问题。

您要首先检查该键是否存在(如果不存在,则等效地跳过循环的一个迭代):

if "grp_farmerreg/farmerdetails/farmermobile" not in json_data:
    continue   # not interested ==> skip to next file 

然后,如果键存在,则将其与所需的值进行比较:

if json_data["grp_farmerreg/farmerdetails/farmermobile"] != "0753693707"
    continue  # not interested => skip to next file

如果仍然没有跳过,则只需将新值添加为

json_data["grp_farmerreg/farmerdetails/farmermobile"] = your new value