Write Multiple JSON Objects To A File In Python 3


multiple json objects to a file
Intermediate

Whether you have experience in coding or you are just starting to learn how to code, you must have heard of JSON data files, and it’s commonly used in coding that you found it very common. It’s a great way to store complex structures and also keep them in persistent storage such as in files as it becomes very readable to edit outside of your application.

What are JSON files and how do you use them

Well JSON is just an abbreviation of (JavaScript Object Notation). It’s a javascript programming language based type of file that is easy to read and understood by humans, and computers, and can be used in python, and other languages as well. The main use of JSON files is to transmit data between a server and an application. In the old days XML files were a popular alternative for human readable files, but they tended to have larger files due to the opening and closing tags, and were more complex to process. JSON files are much simpler and there are many languages with built in methods to process them easily. Python is no exception, there are several ways to process JSON files quite easily.

Let’s dive deeper and find out How to write multiple JSON objects to file in python 3. To answer this question we would like first to demonstrate the steps of opening JSON type files in python and then how to write JSON objects in python.

Reading a JSON File

A JSON files has the following format:

{
  "vehicle_type": "car",
  "make": "Toyota",
  "model": "Prius",
  "year": "2011",
  "license_plate" : "LX4590",
  "previous_owners": [ 
                       {"name" : "richard kimble",
                        "gender" : "male",
                        "age" : 42 },
                       {"name" : "michael scofield",
                        "gender" : "male",
                        "age" : 36 } ]
}

What you see in the above is a combination of single fields and an array with elements in there. A JSON dictionary would have a similar structure.

To open a JSON file in python is relatively easy, first we need to import the json module:

import json

and then to load it in we would

json.load(open('mydatafile.txt'))

Now that we loaded the file in, and if we need to open the file with the open method notice that it opens the file read mode, so no need to put the (‘r’) after the file. However if you want to make changes in the file, will need to add the (‘w’) or (‘a’) .

Converting to a JSON from Python objects

Moving on to the next step, where we need to write JSON objects in python, we use the json.dumps()

Let’s assume that we have a list its name is example_list example_list = ['e', 'o', 1 , 2, 3] and we would like to add it in a JSON file that we already have, this will be by using the json.dumps() like that

json.dumps(example_list)

And this will be the same with any type of objects that you would like to add in JSON strings and will be the same for dictionaries, lists, tuples, strings, int, float, True, False, and None.

However, we need first to use the dumps() to convert any of these types into JSON objects (JavaScript) and to know what are the equivalent types in JSON files check Figure 2

Figure 2

The output is fairly intuitive except the last few that can be a bit surprising. This means if you run print(json.dumps(True)) the out put will be true you can see this in Figure 3

Figure 3

and we get this out put as Figure 4 shows

Figure 4

So, when we use the json.dumps(example_list) that we used earlier the output will change the list to an array so we can add it to JSON file.

Writing JSON to a file

Now we know what .dumps() do, let’s add the object into JSON file and to do so we can use the open() function, but we prefer to use the with : statement for better handling for the JSON file, and it also closes the file and saves the added data, and it’s syntax:

with open ('mydatafile.txt') as j_file:

Keep in mind that if you don’t add the ‘w’ after the file it will automatically be read only and you will not be able to modify or add anything to it. And if we use the normal .open() statement we will have to close the file with .close() statement, now lets create a JSON file and add multiple objects into it.

Creating JSON file and adding multiple objects

To create a JSON file with the with : statement, first we will create a list and a dictionary to help us demonstrate the whole idea:

add_list = [0, 5, 9]
add_dict = { 'word':50, 'line':10, 'page':5 }

Let’s use the same file as the one that we used before mydatafile.txt. For the code that we need to write the add_list and add_dict into the our JSON file and this code will be as follows:

import json
with open ('mydatafile.txt', 'w') as jf:
     list = json.dumps(add_list)
     dict = json.dumps(add_dict)
     jf.write(list)
     jf.write('\n')
     jf.write(dict)

This will create a file with add_list and add_dict as an array and an object to be used as JSON file and we can see the code and the out put in the figures 5 and 6

Figure 5
Figure 6

So, to add multiple objects into JSON file we need to convert it’s type and this can be done easily by json.dumps(), but what if we want to add something like another list instead of the list that’s already there, for instance, lets say we want to change the add_list with the new_list = [1, 6, 7] we will have to brake the JSON file into lines by .readlines will look like this

import json
with open('mydatafile.txt', 'r') as jf:
    lines = j.readlines()

Then we will work on the line that we want to change and in this case we want to change the first line so we will work on lines[0]

lines[0] = new_list + '\n'

Now we open the file normally and add the new lines by .writelines method, just like that

with open('mydatafile.txt', 'w') as jf:
   jf.writelines(lines)

And the output will be the modified JSON file that we wanted to create as shown in Figure 7

Figure 7

With the same concept we can read lines or reach multiple objects on JSON file, it’s by using lines[ ]after reading the file and if there is nested lists or dictionaries we can reach all the objects we want by reading the file first and then lines [ ] [ "str"].

Conclusion

We have covered what is JSON files and that they are display as dictionaries in python, then we moved into how to open a JSON file and how to create one and how to modify and add into the file using different kind of methods. We know for a fact that JSON is a serialized data structure, it is not plain text. So the easy answer for our main question of ‘How to write multiple JSON objects to file in python 3’ is that you need to break the JSON file and to know the index of the place you want to add the object in because as we mentioned before that JSON files are structured, unless you want to add at the end of the JSON or in the beginning.

Subscribe to our newsletter

Get new tips in your inbox automatically. Subscribe to our newsletter!

Leave a Reply

Your email address will not be published. Required fields are marked *

Recent Content