Python read file with json

How to Parse JSON in Python

Invicti Web Application Security Scanner – the only solution that delivers automatic verification of vulnerabilities with Proof-Based Scanning™.

JSON is a popular format for data exchange. Python ships with a built-in JSON module to parse and work with JSON data. And this tutorial will teach you all about working with JSON in Python.

By the end of this tutorial, you’ll have learned:

  • the basics of JSON,
  • how to parse and create JSON strings in Python, and
  • how to read from and write to JSON files in Python.

What is JSON?

JSON stands for J ava S cript O bject N otation, and it’s a text-based format for data interchange. Though JSON is initially inspired by JavaScript objects, almost all programming languages support working with JSON.

If you’ve ever worked with APIs or read through configuration files—you’d likely have run into JSON.

📑 You send and receive data in JSON when querying APIs. And JSON is also widely used in client-server communication in software applications. In addition, you can use JSON for general-purpose data storage as well.

Читайте также:  Python singleton with init

The format of JSON is very similar to that of a Python dictionary. Dictionaries are powerful built-in data structures in Python that store data in key-value pairs.

Before we go any further, here are a few points worth noting:

  • In Python, a JSON object is stored as a dictionary.
  • An array in JSON is stored as a Python list.
  • In JSON, the Boolean values are denoted as true and false . In Python, these are converted to the Booleans True and False .

For more details on the data types that are translated from JSON to Python, read the docs here.

As the json module is part of the Python standard library, you don’t have to install it. You can import into your current directory, like this:

How to Load a JSON String in Python

The general syntax to load a JSON string in Python is:

  • is the Python dictionary to which you’d like to load the JSON string,
  • is any valid JSON string.

This loads the into the Python dictionary .

Let’s code an example. Here json_str is a JSON string.

And the code snippet below shows how you can load the JSON string json_str into a Python dictionary using the loads() method. You can use the built-in type() function to verify that py_dict is a Python dictionary.

py_dict = json.loads(json_str) type(py_dict) # Output: dict print(py_dict) # Output , ]>

As shown in the above code, all fields in the JSON string are key-value pairs in py_dict .

How to Create JSON Strings in Python

Let’s suppose you have a Python dictionary. So how do you create a JSON string from it?

You can do it using the dumps() method with this syntax:

  • is the Python dictionary from which you’d like to create the JSON string,
  • is the resultant JSON string.

So the dumps() method dumps into a JSON string .

To our existing Python dictionary py_dict . let’s add a new key «movies» . You can do it as shown in the following code snippet:

Now, let’s dump the modified dictionary to a new JSON string json_str2 using the dumps() method.

json_str2 = json.dumps(py_dict) print(json_str2) # Output , ], "movies": []> 

As you can see in the above example, the output JSON string is difficult to read through without proper formatting. You can use the optional parameter indent to add indentation.

And you can do this by setting indent to an integer like 2, as shown below:

json_str2 = json.dumps(py_dict, indent = 2) print(json_str2) # Output < "books": [ < "title": "The Wind in the Willows", "author": "Kenneth Grahame", "year": "1908" >, < "title": "To the Lighthouse", "author": "Virginia Woolf", "year": "1927" >], "movies": [ < "title": "The Imitation Game", "year": "2014", "lang": "en", "watched": true >] >

Observe how the output has been formatted with indentation, and it’s easy to follow through.

Note: 💡 If you want the keys to be sorted in alphabetical order, you can set the sort_keys parameter to True .

As you can see in the code snippet below, the keys have now been sorted in alphabetical order.

json_str2 = json.dumps(py_dict, indent = 2, sort_keys=True) print(json_str2) # Output < "books": [ < "author": "Kenneth Grahame", "title": "The Wind in the Willows", "year": "1908" >, < "author": "Virginia Woolf", "title": "To the Lighthouse", "year": "1927" >], "movies": [ < "lang": "en", "title": "The Imitation Game", "watched": true, "year": "2014" >]

And the keys now appear in alphabetical order: «author» , «title» and «year» .

So far, you’ve learned how to work with JSON strings in Python. In the next section, you’ll learn how to work with JSON files.

How to Read a JSON File in Python

To read a JSON file in Python, use the following syntax:

json.load() # where is any valid JSON file.

Notice how we use the load() method and not the loads() method. loads() loads a JSON string, while load() loads a JSON file.

You should consider using context managers when working with files in Python. You can also try to read files as follows, without using context manager:

my_file = open('students.json','r') contents = my_file.read() print(contents) file.close()

If you don’t close the file, there can be a potential wastage of resources.

However, when working with context managers, the files are automatically closed once the file operations are complete.

And you can use context manager to read files, as shown below:

with open('students.json','r') as file: data = json.load(file) print(data) # Output , ]>

As you’re reading from a file, specify the mode as read—indicated by ‘r’ in the above code.

Note: In order to navigate easily through the current directory, please ensure that the JSON file is in the same folder as main.py , as shown in the image below. If your JSON file is in a different folder be sure to specify the path to the file.

read-json-file-in-python

In the next section, you’ll learn how to write to a JSON file.✍

How to Write to a JSON File in Python

To write to an existing JSON file or to create a new JSON file, use the dump() method as shown:

json.dump(,) # where is a Python dictionary # and is the JSON file 

So the above syntax dumps the dictionary into the JSON file .

In the previous section, we had the dictionary py_dict . Now let’s dump that into a new JSON file. And let’s name it new_file.json .

And the following code cell shows how you can use the dump() function:

with open('new_file.json','w') as file: json.dump(py_dict,file)

Note: Opening a file in the write mode ( w ) overwrites the content if the file exists. If the file doesn’t exist, the file is created.

After executing the above code cell, you’ll see that a new JSON file has been created in the current working directory. And you can go ahead and examine the contents of the JSON file.

create-json-file-python

When writing to files, the key goal is data storage. And if you’d like to preserve formatting, you can also use the indent and sort_keys parameters.

Conclusion

⏲ It’s time for a quick summary.

In this tutorial, you have learned:

  • the basics of using JSON,
  • how to use the loads() and load() methods to read JSON string and JSON files respectively,
  • how to use the dumps() and dump() methods to dump Python dictionaries into JSON strings and JSON files respectively.

I hope you found this tutorial helpful. Happy learning!

Источник

Loading a JSON File in Python – How to Read and Parse JSON

Dillion Megida

Dillion Megida

Loading a JSON File in Python – How to Read and Parse JSON

In this article, you’ll learn how to read and parse JSON in Python.

What is JSON?

JSON is short for JavaScript Object Notation. It’s a simple syntax for storing data in name-value pairs. Values can be different data types as long as they are valid. Non-acceptable types for JSON include functions, dates, and undefined .

JSON files are stored with the .json extension with a valid JSON structure.

Here’s what the structure of a JSON file looks like:

You’ll often use JSON to send and receive data from a server in web applications.

When the data is received, the program reads and parses the JSON to extract specific data. Different languages have their own methods for doing this. We’ll look at how to do these in Python here.

How to Read JSON Files

Let’s say the JSON in the code block above is stored in a user.json file. Using the open() inbuilt function in Python, we can read that file and assign the content to a variable. Here’s how:

with open('user.json') as user_file: file_contents = user_file.read() print(file_contents) #

You pass the file path to the open method which opens the file and assigns the stream data from the file to the user_file variable. Using the read method, you can pass the text contents of the file to the file_contents variable.

I used with at the beginning of the expression so that after reading the contents of the file, Python can close the file.

file_contents now contains a stringified version of the JSON. As a next step, you can now parse the JSON.

How to Parse JSON

Python has in-built modules for various operations. For managing JSON files, Python has the json module.

This module comes with many methods. One of which is the loads() method for parsing JSON strings. Then, you can assign the parsed data to a variable like this:

import json with open('user.json') as user_file: file_contents = user_file.read() print(file_contents) parsed_json = json.loads(file_contents) #

Using the loads() method, you can see that the parsed_json variable now has a valid dictionary. From this dictionary, you can access the keys and values in it.

Also notice how null from the JSON is converted to None in python. This is because null is not valid in Python .

How to Use json.load() to Read and Parse JSON Files

The json module also has the load method which you can use to read a file object and parse it at the same time. Using this method, you can update the previous code to this:

import json with open('user.json') as user_file: parsed_json = json.load(user_file) print(parsed_json) #

Instead of using the read method of the file object and using the loads method of the json module, you can directly use the load method which reads and parses the file object.

Wrapping Up

JSON data is commonly known for its simple structure and is popular (a standard in most cases) for information exchange between servers and clients.

Different languages and technologies can read and parse JSON files in different ways. In this article, we’ve learned how to read JSON files and parse such files using the read method of file objects, and the loads and load methods of the json module.

Dillion Megida

Dillion Megida

Developer Advocate and Content Creator passionate about sharing my knowledge on Tech. I simplify JavaScript / ReactJS / NodeJS / Frameworks / TypeScript / et al My YT channel: youtube.com/c/deeecode

If you read this far, tweet to the author to show them you care. Tweet a thanks

Learn to code for free. freeCodeCamp’s open source curriculum has helped more than 40,000 people get jobs as developers. Get started

freeCodeCamp is a donor-supported tax-exempt 501(c)(3) charity organization (United States Federal Tax Identification Number: 82-0779546)

Our mission: to help people learn to code for free. We accomplish this by creating thousands of videos, articles, and interactive coding lessons — all freely available to the public. We also have thousands of freeCodeCamp study groups around the world.

Donations to freeCodeCamp go toward our education initiatives, and help pay for servers, services, and staff.

Источник

Оцените статью