Creating and updating JSON with Python

Let’s start the year with a small piece of code that I could not figure out right off the bat.

It is a side piece of my Web-Scraping series, as manipulating JSONs is a big part of that.

Now with code highlighting!

The problem here is that in order to succesfull json.load(file) this file needs to content at least two brackets {}. The easiest way is to create such file before trying to populate it with data.

file_path = 'my.json'
if not os.path.isfile(file_path):
	with open(file_path , 'w+', encoding='utf-8') as f:
		f.write('{"something": 0}')

Basically I check if a file does not exists and open it with w+. I don’t want it to be rewritten if it already exists and contain data I wrote. If there is a finer solution, please share. This whole task tortured me enough to seek it myself.

Don’t forget to use encoding=’utf-8′ if you have any letters but a-Az-Z.

I populated the file with {"something": 0} in case you want to use jsondata.update() – it requires for both dicts to be no empty.

And then open this file with r+ and update it.

with open(PATH, 'r+', encoding='utf-8') as jsonFile:
	data = json.load(jsonFile)

        # updating

	json.dump(data, jsonFile, ensure_ascii=False)

In order to properly rewrite the file, after you read it, you need to jump back to the 0 symbol and cut everything off.

As I have already said, you can do two things. If you just want to add keys with their respective values, use data.update(). If not and you want to, say, update a list value of a key, it won’t happen – data.update() will just replace the old list with the new list. So iterate your keys and update the old dictionary the good old way.

One thought on “Creating and updating JSON with Python

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.