Python Read File From S3 and Parse Json
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 about how to read and write JSON files using only Python.
Article Map
In order for you to have an overview of this commodity, I wrote this little map that volition prove you lot what nosotros volition do 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 main usage of JSON files and objects.
- Alternative usage of JSON files.
At present that you have seen more specifically what nosotros will do in the tutorial, you are ready to start working with JSON using Python.
Python's JSON Module
To work with JSON ( s tring or file containing JSON object), you tin utilize Python's json
module. Y'all need to import the module before you can use information technology.
Here there is the official JSON documentation, but before going to the side by side stride, I would like to show you the 2 functions of the json
module that we volition use most:
-
json.load()
-
json.dump()
Every bit you lot may have guessed, we will employ the load()
function to read JSON files, and the dump()
one to write JSON files.
Example of a JSON File
In gild for y'all to understand how JSON works, hither is an instance of a simple JSON file:
JSON's Structure
The JSON syntax is derived from JavaScript's object notation syntax:
- Data is in name/value pairs.
- Data is separated past commas.
- Curly braces hold objects.
- Square brackets agree arrays.
In JSON, values must be ane of the following data types:
- A string
- A number
- An object (JSON object)
- An array
- A boolean
- Zero
In JavaScript, values tin be all of the above, plus any other valid JavaScript expression, including:
- A role
- A date
- Undefined
JSON has the following syntax.
- Objects are enclosed in braces (
{}
), their name-value pairs are separated past a comma (,
), and the name and value in a pair are separated past a colon (:
). Names in an object are strings, whereas values may exist 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 array may exist of a different type, including another array or an object. - When objects and arrays comprise other objects or arrays, the data has a tree-like structure
Reading a JSON File
To read a JSON file, every bit nosotros already saw, we will be using the json.load()
function. But before we load some JSON objects from a file, we take to read information technology using Python'due south open()
built-in office.
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()
built-in office. - Load the JSON object inside the
test.json
file using thejson.load()
office. - Impress out the values of the JSON object inside 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'south proper noun. - The
"v.0"
value corresponds to the"overall"
value's proper noun. - The
"I really dearest this computer"
value corresponds to the "text" value's proper noun.
That said, we can start to build our read_json.py
script:
- Line 1: Imported
json
module. - Line 4: Opened
test.json
file using theopen()
function. - Line v: Loaded JSON object using
json.load()
part. - Line 6: Close
examination.json
file. - Lines 8-10: Obtaining the JSON object's values by using the value-proper noun.
- Lines 12-fourteen: Printed JSON object'southward values.
Writing a JSON File
Subsequently learning to read JSON files, we acquire how to write JSON files.
Equally previously said, to write a JSON file with a JSON object inside, we will use the json.dump()
function.
Let's start building our write_json.py
file:
- Line 1: Imported
json
module. - Lines 4-8: Divers our JSON data.
- Line 10: Opened the
writed_json.py
file in write mode ("writed_json.json", "w"
), this means that if the file already exists, the script will only write the JSON data into the file, otherwise, if the file doesn't be, the script volition 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.
After that, open your writed_json.json
file and you should see the post-obit:
Which is exactly the JSON object nosotros defined in the write_json.py
script.
Master Usage of JSON
The JSON format is often used for serializing and transmitting structured data over a network connection. Information technology is used primarily to transmit data between a server and web application, serving as an alternative to XML.
Alternative Usage of JSON
Although it is non its main employ, I take found JSON files very useful for managing the settings of a project in a more than orderly way.
In fact, thanks to its construction, JSON can be hands read and written (as we saw in this tutorial), so I found them a skilful 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 fundamental resources for web developers. But information technology can be useful fifty-fifty for a software engineer or a data scientist, as nosotros saw in the alternative usage of JSON paragraph.
Anyhow, learning new things is e'er good, especially if it takes but a few minutes.
mcneilhartatied1956.blogspot.com
Source: https://betterprogramming.pub/how-to-work-with-json-files-in-python-bedb5b37cbc9
0 Response to "Python Read File From S3 and Parse Json"
Post a Comment