Read File Pass in Size of File

Python Read JSON File – How to Load JSON from a File and Parse Dumps

Welcome! If you want to learn how to work with JSON files in Python, then this article is for you.

You volition learn:

  • Why the JSON format is so important.
  • Its basic structure and information types.
  • How JSON and Python Dictionaries work together in Python.
  • How to work with the Python built-injson module.
  • How to convert JSON strings to Python objects and vice versa.
  • How to use loads() and dumps()
  • How to indent JSON strings automatically.
  • How to read JSON files in Python using load()
  • How to write to JSON files in Python using dump()
  • And more!

Are you ready? Let's begin! ✨

🔹 Introduction: What is JSON?

image-98

The JSON format was originally inspired by the syntax of JavaScript (a programming language used for spider web development). Just since and then it has go a linguistic communication-independent data format and nigh of the programming languages that we use today can generate and read JSON.

Importance and Employ Cases of JSON

JSON is basically a format used to shop or represent data. Its mutual use cases include web evolution and configuration files.

Let's come across why:

  • Web Evolution: JSON is commonly used to send data from the server to the client and vice versa in spider web applications.
image-65
  • Configuration files: JSON is also used to store configurations and settings. For instance, to create a Google Chrome App, you need to include a JSON file called manifest.json to specify the name of the app, its clarification, current version, and other properties and settings.
image-99

🔸 JSON Structure and Format

Now that you know what the JSON format is used for, let's see its basic structure with an example that represents the data of a pizza lodge:

                  {  	"size": "medium", 	"price": 15.67, 	"toppings": ["mushrooms", "pepperoni", "basil"], 	"extra_cheese": false, 	"commitment": true, 	"client": { 		"proper name": "Jane Doe", 		"phone": null, 		"e-mail": "janedoe@e-mail.com" 	} }                
Sample .json file

These are the main characteristics of the JSON format:

  • There is a sequence of fundamental-value pairs surrounded by curly brackets {}.
  • Each key is mapped to a particular value using this format:
                "cardinal": <value>                              

💡 Tip: The values that require quotes have to be surrounded past double quotes.

  • Key-value pairs are separated past a comma. Only the last pair is not followed by a comma.
                { 	"size": "medium", # Comma! 	"cost": 15.67 }              

💡 Tip: Nosotros typically format JSON with dissimilar levels of indentation to brand the data easier to read. In this article, y'all will larn how to add the indentation automatically with Python.

JSON Data Types: Keys and Values

JSON files have specific rules that determine which data types are valid for keys and values.

  • Keys must be strings.
  • Values can exist either a cord, a number, an array, a boolean value (true/ imitation), null, or a JSON object.

According to the Python Documentation:

Keys in fundamental/value pairs of JSON are always of the type str. When a dictionary is converted into JSON, all the keys of the lexicon are coerced to strings.

Style Guide

According to the Google JSON Way Guide:

  • Always choose meaningful names.
  • Array types should accept plural key names. All other key names should be atypical. For instance: use "orders" instead of "social club" if the corresponding value is an array.
  • There should exist no comments in JSON objects.

🔹 JSON vs. Python Dictionaries

JSON and Dictionaries might wait very similar at get-go (visually), simply they are quite dissimilar. Let'south see how they are "connected" and how they complement each other to make Python a powerful tool to work with JSON files.

JSON is a file format used to correspond and store data whereas a Python Dictionary is the actual information construction (object) that is kept in retention while a Python program runs.

How JSON and Python Dictionaries Piece of work Together

image-100

When nosotros work with JSON files in Python, we can't just read them and use the data in our plan directly. This is because the entire file would be represented every bit a single string and we would not be able to access the primal-value pairs individually.

Unless...

Nosotros use the key-value pairs of the JSON file to create a Python dictionary that we tin can use in our program to read the data, employ it, and modify it (if needed).

This is the main connectedness between JSON and Python Dictionaries. JSON is the cord representation of the information and dictionaries are the actual data structures in retention that are created when the program runs.

Great. Now that yous know more nigh JSON, let'south get-go diving into the practical aspects of how you can work with JSON in Python.

🔸 The JSON Module

Luckily for u.s., Python comes with a congenital-in module called json. Information technology is installed automatically when you install Python and it includes functions to help you piece of work with JSON files and strings.

Nosotros will employ this module in the coming examples.

How to Import the JSON Module

To use json in our program, we just need to write an import statement at the top of the file.

Like this:

image-73

With this line, you lot will have admission to the functions divers in the module. We will employ several of them in the examples.

💡 Tip: If you lot write this import statement, you will need to use this syntax to call a function defined in the json module:

image-76

🔹 Python and JSON Strings

To illustrate how some of the most of import functions of the json module work, we will use a multi-line string with JSON format.

JSON String

Peculiarly, we will use this string in the examples. Information technology is only a regular multi-line Python cord that follows the JSON format.

                  data_JSON =  """ { 	"size": "Medium", 	"price": 15.67, 	"toppings": ["Mushrooms", "Extra Cheese", "Pepperoni", "Basil"], 	"client": { 		"proper name": "Jane Doe", 		"telephone": "455-344-234", 		"email": "janedoe@electronic mail.com" 	} } """                
JSON String
  • To define a multi-line string in Python, we use triple quotes.
  • So, we assign the string to the variable data_JSON.

💡 Tip: The Python Manner Guide recommends using double quote characters for triple-quoted strings.

JSON String to Python Dictionary

We will utilize the string with JSON format to create a Python lexicon that nosotros can access, piece of work with, and change.

To practice this, we will employ the loads() function of the json module, passing the cord as the statement.

This is the basic syntax:

image-77

Here is the code:

                # Import the module import json  # String with JSON format data_JSON =  """ { 	"size": "Medium", 	"price": 15.67, 	"toppings": ["Mushrooms", "Actress Cheese", "Pepperoni", "Basil"], 	"client": { 		"name": "Jane Doe", 		"telephone": "455-344-234", 		"email": "janedoe@e-mail.com" 	} } """  # Convert JSON cord to lexicon data_dict = json.loads(data_JSON)                              

Allow's focus on this line:

                data_dict = json.loads(data_JSON)              
  • json.loads(data_JSON) creates a new dictionary with the key-value pairs of the JSON string and it returns this new dictionary.
  • Then, the lexicon returned is assigned to the variable data_dict.

Awesome! If we print this dictionary, we see this output:

                {'size': 'Medium', 'price': 15.67, 'toppings': ['Mushrooms', 'Extra Cheese', 'Pepperoni', 'Basil'], 'client': {'name': 'Jane Doe', 'phone': '455-344-234', 'email': 'janedoe@e-mail.com'}}              

The lexicon has been populated with the data of the JSON string. Each primal-value pair was added successfully.

Now let'south run into what happens when nosotros endeavor to access the values of the key-value pairs with the same syntax that we would utilise to access the values of a regular Python dictionary:

                print(data_dict["size"]) print(data_dict["toll"]) print(data_dict["toppings"]) print(data_dict["client"])              

The output is:

                Medium 15.67 ['Mushrooms', 'Extra Cheese', 'Pepperoni', 'Basil'] {'proper name': 'Jane Doe', 'telephone': '455-344-234', 'electronic mail': 'janedoe@email.com'}              

Exactly what we expected. Each central can be used to admission its corresponding value.

💡 Tip: Nosotros can utilize this lexicon just like any other Python dictionary. For example, we can phone call dictionary methods, add together, update, and remove key-value pairs, and more than. We can even use it in a for loop.

JSON to Python: Type Conversion

When you use loads() to create a Python lexicon from a JSON string, yous will detect that some values volition be converted into their corresponding Python values and data types.

This table presented in the Python Documentation for the json module summarizes the correspondence from JSON data types and values to Python data types and values:

image-79
Table presented in the official documentation of the json module

💡 Tip: The same conversion tabular array applies when we work with JSON files.

Python Dictionary to JSON Cord

At present you know how to create a Python dictionary from a string with JSON format.

But sometimes we might need to practise exactly the opposite, creating a string with JSON format from an object (for case, a dictionary) to print it, display it, store it, or work with it as a cord.

To do that, we can use the dumps function of the json module, passing the object as statement:

image-80

💡 Tip: This role will render a string.

This is an example where we catechumen the Python dictionary client into a string with JSON format and shop information technology in a variable:

                # Python Dictionary client = {     "name": "Nora",     "age": 56,     "id": "45355",     "eye_color": "green",     "wears_glasses": Simulated }  # Get a JSON formatted string client_JSON = json.dumps(customer)              

Permit's focus on this line:

                client_JSON = json.dumps(client)              
  • json.dumps(customer) creates and returns a string with all the key-value pairs of the lexicon in JSON format.
  • Then, this cord is assigned to the client_JSON variable.

If we print this string, we see this output:

                {"name": "Nora", "age": 56, "id": "45355", "eye_color": "light-green", "wears_glasses": false}              

💡 Tip: Detect that the concluding value (false) was inverse. In the Python dictionary, this value was False only in JSON, the equivalent value is faux. This helps u.s.a. confirm that, indeed, the original dictionary is at present represented as a string with JSON format.

If we check the data blazon of this variable, nosotros come across:

                <class 'str'>              

So the return value of this function was definitely a string.

Python to JSON: Blazon Conversion

A process of blazon conversion occurs as well when we convert a dictionary into a JSON string. This table from the Python Documentation illustrates the respective values:

image-81
Table from the official documentation of the json module.

How to Print JSON With Indentation

If we use the dumps function and nosotros print the string that we got in the previous example, we encounter:

                {"name": "Nora", "age": 56, "id": "45355", "eye_color": "green", "wears_glasses": false}              

But this is not very readable, right?

We tin can improve the readability of the JSON string by calculation indentation.

To do this automatically, nosotros just need to pass a second statement to specify the number of spaces that we want to use to indent the JSON string:

image-111

💡 Tip: the second argument has to exist a non-negative integer (number of spaces) or a string. If indent is a cord (such as "\t"), that string is used to indent each level (source).

At present, if we call dumps with this second argument:

                client_JSON = json.dumps(client, indent=iv)              

The outcome of printing client_JSON is:

                {     "name": "Nora",     "age": 56,     "id": "45355",     "eye_color": "light-green",     "wears_glasses": false }              

That's corking, correct? Now our string is nicely formatted. This volition be very helpful when we start working with files to shop the data in a human-readable format.

How to Sort the Keys

You can besides sort the keys in alphabetical order if you demand to. To practise this, you just need to write the name of the parameter sort_keys and laissez passer the value True:

image-84

💡 Tip: The value of sort_keys is Faux by default if you don't pass a value.

For example:

                client_JSON = json.dumps(client, sort_keys=True)              

Returns this string with the keys sorted in alphabetical society:

                {"age": 56, "eye_color": "greenish", "id": "45355", "name": "Nora", "wears_glasses": false}              

How to Sort Alphabetically and Indent (at the aforementioned fourth dimension)

To generate a JSON string that is sorted alphabetically and indented, you just need to pass the 2 arguments:

image-104

In this case, the output is:

                {     "age": 56,     "eye_color": "green",     "id": "45355",     "proper noun": "Nora",     "wears_glasses": false }              

💡 Tip: You can pass these arguments in whatsoever order (relative to each other), simply the object has to be the first argument in the listing.

Great. Now you know how to work with JSON strings, and then permit'southward see how you tin work with JSON files in your Python programs.

🔸 JSON and Files

Typically, JSON is used to store data in files, and then Python gives us the tools we need to read these types of file in our program, piece of work with their information, and write new data.

💡 Tip: a JSON file has a .json extension:

image-62

Let's see how we tin can work with .json files in Python.

How to Read a JSON File in Python

Allow's say that we created an orders.json file with this data that represents two orders in a pizza shop:

                  { 	"orders": [  		{ 			"size": "medium", 			"cost": 15.67, 			"toppings": ["mushrooms", "pepperoni", "basil"], 			"extra_cheese": false, 			"delivery": truthful, 			"client": { 				"name": "Jane Doe", 				"phone": null, 				"email": "janedoe@email.com" 			} 		}, 		{ 			"size": "small", 			"cost": 6.54, 			"toppings": null, 			"extra_cheese": true, 			"delivery": faux, 			"customer": { 				"proper name": "Foo Jones", 				"phone": "556-342-452", 				"email": null 			} 		} 	] }                
orders.json

Please accept a moment to clarify the structure of this JSON file.

Here are some quick tips:

  • Notice the data types of the values, the indentation, and the overall structure of the file.
  • The value of the principal key "orders" is an array of JSON objects (this array will be represented equally list in Python). Each JSON object holds the data of a pizza order.

If nosotros desire to read this file in Python, we just need to use a with statement:

image-87

💡 Tip: In the syntax to a higher place, we can assign any name to file (light-green box). This is a variable that we can employ within the with argument to refer to the file object.

The key line of code in this syntax is:

                data = json.load(file)              
  • json.load(file) creates and returns a new Python dictionary with the key-value pairs in the JSON file.
  • So, this lexicon is assigned to the data variable.

💡 Tip: Discover that we are using load() instead of loads(). This is a unlike part in the json module. You will learn more almost their differences at the end of this article.

Once we have the content of the JSON file stored in the data variable equally a lexicon, we can use information technology to do basically anything nosotros desire.

Examples

For instance, if we write:

                print(len(data["orders"]))              

The output is 2 because the value of the main key "orders" is a list with two elements.

We can also utilise the keys to admission their corresponding values. This is what we typically do when nosotros piece of work with JSON files.

For case, to access the toppings of the first order, we would write:

                data["orders"][0]["toppings"]              
  • First, nosotros select the chief key "orders"
  • Then, we select the first element in the list (index 0).
  • Finally, we select the value that corresponds to the key "toppings"

Y'all can encounter this "path" graphically in the diagram:

image-101

If we print this value, the output is:

                ['mushrooms', 'pepperoni', 'basil']              

Exactly what we expected. Y'all only demand to "dive deeper" into the structure of the lexicon by using the necessary keys and indices. Yous can use the original JSON file/string as a visual reference. This way, you can access, modify, or delete any value.

💡 Tip: Remember that we are working with the new dictionary. The changes fabricated to this lexicon will non affect the JSON file. To update the content of the file, nosotros need to write to the file.

How to Write to a JSON File

Let's meet how you can write to a JSON file.

The first line of the with argument is very similar. The simply change is that you need to open the file in 'w' (write) mode to be able to alter the file.

image-105

💡 Tip: If the file doesn't exist already in the current working directory (folder), it volition be created automatically. By using the 'due west' mode, we volition be replacing the entire content of the file if it already exists.

At that place are ii alternative ways to write to a JSON file in the torso of the with statement:

  • dump
  • dumps

Let'south see them in particular.

First Approach: dump

This is a function that takes two arguments:

  • The object that volition be stored in JSON format (for case, a dictionary).
  • The file where it will be stored (a file object).
image-91

Let's say that the pizza shop wants to remove the clients' information from the JSON file and create a new JSON file chosen orders_new.json with this new version.

Nosotros can do this with this lawmaking:

                # Open the orders.json file with open("orders.json") equally file:     # Load its content and make a new lexicon     data = json.load(file)      # Delete the "client" key-value pair from each order     for order in information["orders"]:         del club["client"]  # Open (or create) an orders_new.json file  # and store the new version of the data. with open up("orders_new.json", 'due west') as file:     json.dump(data, file)              

This was the original version of the information in the orders.json file. Notice that the "client" key-value pair exists.

                  { 	"orders": [  		{ 			"size": "medium", 			"price": 15.67, 			"toppings": ["mushrooms", "pepperoni", "basil"], 			"extra_cheese": false, 			"delivery": true, 			"client": { 				"name": "Jane Doe", 				"phone": cipher, 				"e-mail": "janedoe@email.com" 			} 		}, 		{ 			"size": "small", 			"cost": vi.54, 			"toppings": null, 			"extra_cheese": true, 			"delivery": false, 			"client": { 				"name": "Foo Jones", 				"phone": "556-342-452", 				"e-mail": null 			} 		} 	] }                                  
orders.json

This is the new version in the orders_new.json file:

                  {"orders": [{"size": "medium", "toll": 15.67, "toppings": ["mushrooms", "pepperoni", "basil"], "extra_cheese": false, "delivery": true}, {"size": "small-scale", "cost": 6.54, "toppings": nothing, "extra_cheese": truthful, "commitment": false}]}                
orders_new.json

If you lot analyze this carefully, you will see that the "clients" primal-value pair was removed from all the orders.

Nonetheless, there is something missing in this file, right?

Please accept a moment to think almost this... What could it exist?

Indentation, of class!

The file doesn't really look similar a JSON file, but nosotros can hands fix this by passing the argument indentation=four to dump().

image-92

Now the content of the file looks similar this:

                  {     "orders": [         {             "size": "medium",             "price": 15.67,             "toppings": [                 "mushrooms",                 "pepperoni",                 "basil"             ],             "extra_cheese": false,             "delivery": true         },         {             "size": "small",             "cost": 6.54,             "toppings": cipher,             "extra_cheese": true,             "commitment": false         }     ] }                
orders_new.json

What a difference! This is exactly what we would expect a JSON file to look like.

Now you know how to read and write to JSON files using load() and dump(). Let'due south run into the differences between these functions and the functions that we used to work with JSON strings.

🔹 load() vs. loads()

This tabular array summarizes the key differences between these ii functions:

image-110

💡 Tip: Think of loads() as "load cord" and that will help you remember which function is used for which purpose.

🔸 dump() vs. dumps()

Hither nosotros have a table that summarizes the key differences between these two functions:

image-109

💡 Tip: Think of dumps() every bit a "dump cord" and that volition help you retrieve which function is used for which purpose.

🔹 Of import Terminology in JSON

Finally, there are 2 important terms that you demand to know to work with JSON:

  • Serialization: converting an object into a JSON string.
  • Deserialization: converting a JSON cord into an object.

🔸 In Summary

  • JSON (JavaScript Object Notation) is a format used to represent and shop information.
  • It is commonly used to transfer data on the spider web and to shop configuration settings.
  • JSON files have a .json extension.
  • You can convert JSON strings into Python objects and vice versa.
  • You tin can read JSON files and create Python objects from their key-value pairs.
  • You can write to JSON files to shop the content of Python objects in JSON format.

I really hope you lot liked my article and found information technology helpful. At present you know how to work with JSON in Python. Follow me on Twitter @EstefaniaCassN and cheque out my online courses.



Larn to code for complimentary. freeCodeCamp's open up source curriculum has helped more than 40,000 people get jobs as developers. Go started

fellbirear1985.blogspot.com

Source: https://www.freecodecamp.org/news/python-read-json-file-how-to-load-json-from-a-file-and-parse-dumps/

0 Response to "Read File Pass in Size of File"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel