Saving JSON Data to a File in Python

In this snippet, I demonstrate how to save a JSON string to a file using Python. The JSON contains basic user information such as name, age, city, and GPA. It's a quick way to persist data in a structured format.
mail@pastecode.io avatar
unknown
python
a month ago
270 B
5
Indexable
Never
# Desafio
import json

desafio_json = """{
    "name": "John Smith",
    "age": 30,
    "city": "New York",
    "isStudent": true,
    "gpa": 3.5
}"""

with open('desafio.json', 'w', encoding='utf8') as arquivo_json:
    json.dump(desafio_json, arquivo_json)
Leave a Comment