1

I am trying to read and write a json file using python script(I have embedded this python block inside a shell script)

test.json

{
  "environments": ["dev", "qa", "load", "prod"],
  "region": ["asia", "europe", "americas", "asia-pacific"],
  "product": ["mobile", "internet", "kiosk", "branch"],
  "ID": ["mobile:111", "internet:222", "kiosk:333", "branch:444"]
}

I am trying to add region(uk), before that I want to check and add if it is not avilable in list. Below is my python code and I am able to update the value

reg_val="uk"

a_dict = {}
try:
    with open('test.json') as data_file:
        data = json.load(data_file)
        temp_list = []
        for dicObj in data["region"]:
            temp_list.append(dicObj)
        temp_list.append(reg_val)
        data["region"] = temp_list
        a_dict["region"] = data["region"]
        with open('test.json','w') as f:
            f.write(json.dumps(a_dict, indent=4, sort_keys=True, encoding="utf-8"))
except IOError as io:
    print "ERROR: ", io

I was able to update the json file, but it removes the other lists.The output I see is

{
    "region": [
        "asia", 
        "europe", 
        "americas", 
        "asia-pacific", 
        "uk"
    ]
}
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
Mowgli
  • 143
  • 2
  • 11

3 Answers3

6

Just keep it simple, and only append if reg_val exists with the in operator. Then you could just dump to another file.

import json

reg_val = "uk"

with open('test.json') as in_file, open('out.json', 'w') as out_file:
    data = json.load(in_file)
    if reg_val not in data['region']:
        data['region'].append(reg_val)

    json.dump(data, out_file, indent=4, sort_keys=True)

Which gives the following out.json:

{
    "ID": [
        "mobile:111",
        "internet:222",
        "kiosk:333",
        "branch:444"
    ],
    "environments": [
        "dev",
        "qa",
        "load",
        "prod"
    ],
    "product": [
        "mobile",
        "internet",
        "kiosk",
        "branch"
    ],
    "region": [
        "asia",
        "europe",
        "americas",
        "asia-pacific",
        "uk"
    ]
}
RoadRunner
  • 25,803
  • 6
  • 42
  • 75
4

It's because you're writing a_dict to the file, which is initialised as an empty dict, then only has data["region"] inserted into it.

I had some code to provide as an example, but RoadRunner beat me to it (hey, he's pretty fast!). You also need to be careful with your file-handling, as you have a READ file handler open at the same time as a WRITE file handler - this can be dangerous.

Ash Hall
  • 458
  • 4
  • 14
3

The reason the other lists were removed is because you're using write mode instead of append mode. When you reopen your test.json file, use the code open('test.json', 'a') to append to test.json without overwriting what you previously wrote.

EDIT: As mentioned in the comments, it is not suitable to append to a json file. Instead, write everything in a single dump. I recommend RoadRunner's approach.

Joel
  • 1,564
  • 7
  • 12
  • 20
  • 1
    But this will append the json representation of the list, including the outer braces (`{`). This will cause errors later. – Levi Lesches Jul 02 '18 at 23:53
  • Good point, I'll edit my post to mention this. – Joel Jul 02 '18 at 23:54
  • I was going to suggest keeping track of the line in question, then re-write the whole thing (including) that line, but @RoadRunner also beat me to it – Levi Lesches Jul 02 '18 at 23:55