Skip to content

Commit 35e2998

Browse files
committed
added working with json files tutorial
1 parent a951f9b commit 35e2998

File tree

6 files changed

+79
-1
lines changed

6 files changed

+79
-1
lines changed

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ This is a repository of all the tutorials of [The Python Code](https://www.thepy
9494
- [How to List all Files and Directories in FTP Server using Python](https://www.thepythoncode.com/article/list-files-and-directories-in-ftp-server-in-python). ([code](python-standard-library/listing-files-in-ftp-server))
9595
- [How to Read Emails in Python](https://www.thepythoncode.com/article/reading-emails-in-python). ([code](python-standard-library/reading-email-messages))
9696
- [How to Download and Upload Files in FTP Server using Python](https://www.thepythoncode.com/article/download-and-upload-files-in-ftp-server-using-python). ([code](python-standard-library/download-and-upload-files-in-ftp))
97+
- [How to Work with JSON Files in Python](https://www.thepythoncode.com/article/working-with-json-files-in-python). ([code](python-standard-library/working-with-json))
9798

9899
- ### [Using APIs](https://www.thepythoncode.com/topic/using-apis-in-python)
99100
- [How to Automate your VPS or Dedicated Server Management in Python](https://www.thepythoncode.com/article/automate-veesp-server-management-in-python). ([code](general/automating-server-management))
@@ -103,7 +104,7 @@ This is a repository of all the tutorials of [The Python Code](https://www.thepy
103104
- [How to Use Google Drive API in Python](https://www.thepythoncode.com/article/using-google-drive--api-in-python). ([code](general/using-google-drive-api))
104105
- [How to Translate Text in Python](https://www.thepythoncode.com/article/translate-text-in-python). ([code](general/using-google-translate-api))
105106

106-
- ### Database
107+
- ### [Database](https://www.thepythoncode.com/topic/using-databases-in-python)
107108
- [How to Use MySQL Database in Python](https://www.thepythoncode.com/article/using-mysql-database-in-python). ([code](database/mysql-connector))
108109
- [How to Connect to a Remote MySQL Database in Python](https://www.thepythoncode.com/article/connect-to-a-remote-mysql-server-in-python). ([code](database/connect-to-remote-mysql-server))
109110
- [How to Use MongoDB Database in Python](https://www.thepythoncode.com/article/introduction-to-mongodb-in-python). ([code](database/mongodb-client))
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# [How to Work with JSON Files in Python](https://www.thepythoncode.com/article/working-with-json-files-in-python)
2+
To run `example.py`, you have to install `requests` library:
3+
- `pip3 install -r requirements.txt`
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import requests
2+
import json
3+
4+
5+
# make API request and parse JSON automatically
6+
data = requests.get("https://jsonplaceholder.typicode.com/users").json()
7+
# save all data in a single JSON file
8+
file_name = "user_data.json"
9+
with open(file_name, "w") as f:
10+
json.dump(data, f, indent=4)
11+
print(file_name, "saved successfully!")
12+
13+
# or you can save each entry into a file
14+
for user in data:
15+
# iterate over `data` list
16+
file_name = f"user_{user['id']}.json"
17+
with open(file_name, "w") as f:
18+
json.dump(user, f, indent=4)
19+
print(file_name, "saved successfully!")
20+
21+
22+
# load 2nd user for instance
23+
file_name = "user_2.json"
24+
with open(file_name) as f:
25+
user_data = json.load(f)
26+
27+
print(user_data)
28+
print("Username:", user_data["username"])
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import json
2+
3+
# read a JSON file
4+
# 1st option
5+
file_name = "data1.json"
6+
with open(file_name) as f:
7+
data = json.load(f)
8+
9+
print(data)
10+
# 2nd option
11+
file_name = "data2.json"
12+
with open(file_name) as f:
13+
data = json.loads(f.read())
14+
15+
print(data)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
requests
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import json
2+
3+
# example dictionary to save as JSON
4+
data = {
5+
"first_name": "John",
6+
"last_name": "Doe",
7+
"email": "john@doe.com",
8+
"salary": 1499.9, # just to demonstrate we can use floats as well
9+
"age": 17,
10+
"is_real": False, # also booleans!
11+
"titles": ["The Unknown", "Anonymous"] # also lists!
12+
}
13+
14+
# save JSON file
15+
# 1st option
16+
with open("data1.json", "w") as f:
17+
json.dump(data, f)
18+
19+
# 2nd option
20+
with open("data2.json", "w") as f:
21+
f.write(json.dumps(data, indent=4))
22+
23+
24+
unicode_data = {
25+
"first_name": "أحمد",
26+
"last_name": "علي"
27+
}
28+
29+
with open("data_unicode.json", "w", encoding="utf-8") as f:
30+
json.dump(unicode_data, f, ensure_ascii=False)

0 commit comments

Comments
 (0)