0

I am trying to update the JSON files being updated into my database using the following python script.

#!/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

The aim of the update script is to find out if "grp_cowmonitoring/rpt_animrec/grp_animrec/cowtagid" lacks within the array on my JSON; Then i will update the same key with a difference on the name "grp_cowmonitoring/rpt_animrec/grp_animrec/damtagid"

original file

{
    "_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", 
    ....
}

Expected JSON file

{
    "_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", 
    ....
}

How can i modify my python script to accommodate the changes in my JSON

Error message after update of the original code

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'
2
  • Your code is on the right track, the only problem is how you are accessing the data, you need to do json_data['grp_cowmonitoring/rpt_animrec'][0] to get to the right data. Commented Jul 11, 2019 at 9:29
  • @palvarez update the code so i get a better picture Commented Jul 11, 2019 at 9:33

1 Answer 1

1

You just need to access the right elements from your 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.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
Sign up to request clarification or add additional context in comments.

7 Comments

i have updated my script but it fails with the error
I have copied it at the bottom of my description @palvarez
That means not all json files have this key "grp_cowmonitoring/rpt_animrec". You could have a condition to make sure you are modifying the right files maybe. Could you give more details about the files you are treating with?
Not all files have this key; all JSON files those with this key "grp_cowmonitoring/rpt_animrec" and those without are under one folder
@MirieriMogaka Also it will help to have the initial and expected output file for example
|

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.