0

I am doing a simple python script to replace a key in my JSON file, the following is my python script

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)
            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

This scripts gives me the following error:

    File "/opt/rdm/adggeth/ADGG-ETH-02/addfidkey.sh", line 16, in <module>
    json_data["grp_farmerdts/hh_id"] = json_data["grp_farmerreg/farmerdetails/farmermobile"]
KeyError: 'grp_farmerreg/farmerdetails/farmermobile'

How can i successfully replace the key of my JSON file

My sample JSON file is as follows: I have deleted most of the details in my JSON file for easier understanding

  {

    "grp_farmerreg/members_no": "1", 
    "grp_farmerreg/hh_country": "1", 
    "_bamboo_dataset_id": "", 
    "_tags": [], 
    "grp_farmerreg/totanim": "6", 
    "gpsloc": "7.0854258 38.6111001 1674.300048828125 17.0", 
    "grp_farmerreg/farmerdetails/farmermobile": "0913886615", 
    "grp_farmerreg/grpdetails2/nmale6to14": "1", 
    "grp_farmerreg/farmerdetails/farmerhhhead": "1", 
    "grp_farmerreg/grpdetails2/nfem15to64": "0", 
    "meta/instanceID": "uuid:008aea99-810e-4dbc-9235-4d02b8e8d36b", 
    "_duration": "",     
    "grp_farmerreg/grpdetails2/nfem6to14": "1", 
    "grp_farmerreg/grp_e1_ctlca/cattletotalowned": "6", 
    "start_time": "2018-12-24T11:24:53.034+03", 
    "_uuid": "008aea99-810e-4dbc-9235-4d02b8e8d36b", 

}

my expected output

{

    "grp_farmerreg/members_no": "1", 
    "grp_farmerreg/hh_country": "1", 
    "_bamboo_dataset_id": "", 
    "_tags": [], 
    "grp_farmerreg/totanim": "6", 
    "gpsloc": "7.0854258 38.6111001 1674.300048828125 17.0", 
    "grp_farmerreg/farmerdetails/farmermobile": "0913886615", 
    "grp_farmerdts/hh_id":"0913886615", 
    "grp_farmerreg/grpdetails2/nmale6to14": "1", 
    "grp_farmerreg/farmerdetails/farmerhhhead": "1", 
    "grp_farmerreg/grpdetails2/nfem15to64": "0", 
    "meta/instanceID": "uuid:008aea99-810e-4dbc-9235-4d02b8e8d36b", 
    "_duration": "",     
    "grp_farmerreg/grpdetails2/nfem6to14": "1", 
    "grp_farmerreg/grp_e1_ctlca/cattletotalowned": "6", 
    "start_time": "2018-12-24T11:24:53.034+03", 
    "_uuid": "008aea99-810e-4dbc-9235-4d02b8e8d36b", 

}

1 Answer 1

1

Try changing the lines

if "grp_farmerreg/farmerdetails/farmermobile" not in json_data:
    json_data["grp_farmerreg/farmerdetails/farmermobile"] = json_data["grp_farmerdts/hh_id"]

with

if "grp_farmerreg/farmerdetails/farmermobile" in json_data:
    json_data["grp_farmerdts/hh_id"] = json_data["grp_farmerreg/farmerdetails/farmermobile"]
    del json_data["grp_farmerreg/farmerdetails/farmermobile"]
Sign up to request clarification or add additional context in comments.

2 Comments

i have tried your solution but i get the same error @Julian Minde
Did you change the first line of my answer as well? The Key error says that the key "grp_farmerdts/hh_id" does not exist in your json_data, which seems correct. However, by assigning it you add it and that should be fine. If you tried this solution and got key error it should mention another key.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.