Appending a JSON item to a JSON list

This post is more a question then an answer.

I started recently working on a Python project, I loved the language. But as a person coming from Java World, the uncertainty (I call it like this) in the use of variables and sometimes the ambiguity in its errors is killing me. Of course probably this is happening ‘cuz I’m still a newbie in this beautiful world.

Now to go to our point. I needed to open a file that contains a JSON List , and append a JSON object to it. I tried many ways to do this, my working solution (which looks so heavy also) is as the following :

→ Read the file as a json fileX list

→ Append our new item

→Dumps the list into string and store it inside again in the fileX

The actual code is as the following :

with open(‘file.json’, ‘r+’) as json_file:

a = json.load(json_file)


with open(‘file.json’, ‘w+’) as json_file:

shhh = json.dumps(a)


This solution works, but is so heavy as I said. I would be happy to see your optimizations for it.

Full Stack Developer| Loading…