Reading From a Json File in Python
How to Work With JSON Files in Python
Read from and write to JSON files using Python
Most of the time, we find JSON objects in a file, which is why today, I will tell you near how to read and write JSON files using but Python.
Article Map
In order for you to accept an overview of this article, I wrote this lilliputian map that will prove y'all what we will practise in this tutorial.
- Overview of the JSON module.
- Example of a JSON file.
- JSON file structure.
- Read JSON using the
json
module. - Write JSON objects in JSON files.
- The chief usage of JSON files and objects.
- Alternative usage of JSON files.
Now that you take seen more specifically what we will practise in the tutorial, yous are fix to start working with JSON using Python.
Python's JSON Module
To work with JSON ( due south tring or file containing JSON object), you lot tin use Python's json
module. You need to import the module before you tin can use it.
Here in that location is the official JSON documentation, but before going to the adjacent step, I would like to show you the 2 functions of the json
module that we will employ most:
-
json.load()
-
json.dump()
Every bit you may take guessed, we will use the load()
function to read JSON files, and the dump()
one to write JSON files.
Case of a JSON File
In social club for you to understand how JSON works, here is an example of a uncomplicated JSON file:
JSON's Structure
The JSON syntax is derived from JavaScript'southward object notation syntax:
- Data is in name/value pairs.
- Data is separated by commas.
- Curly braces hold objects.
- Square brackets hold arrays.
In JSON, values must be one of the following information types:
- A string
- A number
- An object (JSON object)
- An array
- A boolean
- Null
In JavaScript, values can be all of the above, plus any other valid JavaScript expression, including:
- A function
- A date
- Undefined
JSON has the following syntax.
- Objects are enclosed in braces (
{}
), their proper name-value pairs are separated by a comma (,
), and the name and value in a pair are separated past a colon (:
). Names in an object are strings, whereas values may be of any of the seven value types, including another object or an array. - Arrays are enclosed in brackets (
[]
), and their values are separated by a comma (,
). Each value in an assortment may be of a different type, including another array or an object. - When objects and arrays contain other objects or arrays, the data has a tree-like structure
Reading a JSON File
To read a JSON file, as we already saw, we will be using the json.load()
function. Just before we load some JSON objects from a file, we take to read it using Python's open up()
built-in role.
Suppose we want to read the test.json
file using the read_json.py
script, we:
- Import the
json
module. - Open
examination.json
using theopen up()
built-in role. - Load the JSON object inside the
test.json
file using thejson.load()
part. - Print out the values of the JSON object within the
test.json
file.
Before writing our script, we need to create the test.json
file and paste the following code into information technology:
This file contains a JSON object that has 3 values' names and three values, a value for every value-name:
- The
"Macbook Air"
value corresponds to the"product"
value'due south name. - The
"5.0"
value corresponds to the"overall"
value'south name. - The
"I really love this computer"
value corresponds to the "text" value'southward proper name.
That said, we tin can start to build our read_json.py
script:
- Line 1: Imported
json
module. - Line 4: Opened
exam.json
file using theopen()
function. - Line 5: Loaded JSON object using
json.load()
office. - Line 6: Shut
test.json
file. - Lines eight-x: Obtaining the JSON object's values by using the value-proper name.
- Lines 12-14: Printed JSON object's values.
Writing a JSON File
Later learning to read JSON files, nosotros learn how to write JSON files.
Every bit previously said, to write a JSON file with a JSON object inside, we volition use the json.dump()
function.
Allow's commencement edifice our write_json.py
file:
- Line 1: Imported
json
module. - Lines four-eight: Defined our JSON data.
- Line 10: Opened the
writed_json.py
file in write fashion ("writed_json.json", "due west"
), this means that if the file already exists, the script will merely write the JSON data into the file, otherwise, if the file doesn't be, the script will create it and then write the JSON data into it. - Line eleven: Wrote the
json_data
we defined earlier in the"writed_json.json"
file using thejson.dump()
function. - Line 12: Closed the file.
Afterward that, open your writed_json.json
file and you should see the following:
Which is exactly the JSON object we defined in the write_json.py
script.
Primary Usage of JSON
The JSON format is often used for serializing and transmitting structured data over a network connection. It is used primarily to transmit data between a server and web awarding, serving as an alternative to XML.
Culling Usage of JSON
Although it is not its main use, I accept plant JSON files very useful for managing the settings of a projection in a more orderly way.
In fact, thanks to its structure, JSON tin exist hands read and written (every bit we saw in this tutorial), so I plant them a adept resource for managing settings.
Obviously, there are many other usages of JSONs, you can find your own preferred usage!
Conclusion
Learning how to manage JSON is a cardinal resource for web developers. Just it can be useful even for a software engineer or a data scientist, as we saw in the alternative usage of JSON paragraph.
Anyway, learning new things is always good, especially if it takes just a few minutes.
boylesthemake1939.blogspot.com
Source: https://betterprogramming.pub/how-to-work-with-json-files-in-python-bedb5b37cbc9
0 Response to "Reading From a Json File in Python"
Post a Comment