🌐
Python
docs.python.org › 3 › library › json.html
json — JSON encoder and decoder
3 weeks ago - JSON (JavaScript Object Notation), specified by RFC 7159 (which obsoletes RFC 4627) and by ECMA-404, is a lightweight data interchange format inspired by JavaScript object literal syntax (although it is not a strict subset of JavaScript [1] ...
🌐
Real Python
realpython.com › python-json
Working With JSON Data in Python – Real Python
August 20, 2025 - Generally, being cautious about data type conversions should be the concern of the Python program that writes the JSON. With the knowledge you have about JSON files, you can always anticipate which Python data types you’ll end up with as long as the JSON file is valid. If you use json.load(), then the content of the file you load must contain valid JSON syntax.
Discussions

formatting json output in Python
json.dumps(obj, indent=2, sort_keys=False) More on reddit.com
🌐 r/learnpython
16
52
May 12, 2022
python - How do I write JSON data to a file? - Stack Overflow
Basically, I think it's a bug in the json.dump() function in Python 2 only - It can't dump a Python (dictionary / list) data containing non-ASCII characters, even you open the file with the encoding = 'utf-8' parameter. (i.e. No matter what you do). More on stackoverflow.com
🌐 stackoverflow.com
Handling JSON files with ease in Python
Looks good. Considered discussing dataclasses/pydantic with json? I found that these go well together More on reddit.com
🌐 r/Python
55
421
May 29, 2022
How to open contents of JSON file in Python

JSON.load takes a file handle as a parameter, and JSON.loads takes the actual JSON object as a string.

with open('my_json.txt', 'r') as f:
    my_json = JSON.load(f)

or

json_string = '{"thing": "value"}'
my_json = JSON.loads(json_string)
More on reddit.com
🌐 r/learnpython
11
6
June 16, 2016
🌐
W3Schools
w3schools.com › python › python_json.asp
Python JSON
Python Overview Python Built-in ... Q&A Python Bootcamp Python Certificate Python Training ... JSON is a syntax for storing and exchanging data....
🌐
DataCamp
datacamp.com › tutorial › json-data-python
Python JSON Data: A Guide With Examples | DataCamp
December 3, 2024 - Configuration Files. JSON provides a simple and easy-to-read format for storing and retrieving configuration data. This can include settings for the application, such as the layout of a user interface or user preferences. IoT (Internet of Things). IoT devices often generate large amounts of data, which can be stored and transmitted between sensors and other devices more efficiently using JSON. python_obj = { "name": "John Doe", "age": 30, "email": "john.doe@example.com", "is_employee": True, "hobbies": [ "reading", "playing soccer", "traveling" ], "address": { "street": "123 Main Street", "city": "New York", "state": "NY", "zip": "10001" } } print(python_obj)
🌐
freeCodeCamp
freecodecamp.org › news › how-to-use-the-json-module-in-python
How to Use the JSON Module in Python – A Beginner's Guide
June 5, 2023 - The JSON module provides you with a json.dumps() function to serialize Python objects into a JSON formatted string. It provides various options for customization, including formatting the output to make it more human-readable.
🌐
GeeksforGeeks
geeksforgeeks.org › python › reading-and-writing-json-to-a-file-in-python
Reading and Writing JSON to a File in Python - GeeksforGeeks
The JSON package in Python has a function called json.dumps() that helps in converting a dictionary to a JSON object. It takes two parameters: dictionary: the name of a dictionary which should be converted to a JSON object. indent: defines the number of units for indentation · After converting the dictionary to a JSON object, simply write it to a file using the "write" function.
Published   August 5, 2025
🌐
W3Resource
w3resource.com › JSON › snippets › json-file-tutorial.php
JSON File: Structure, Examples, and Python Usage
import json # Import the JSON module # Open and load the JSON file with open('example.json', 'r') as file: data = json.load(file) # Modify the data data["age"] = 35 # Update the age data["skills"].append("Django") # Add a new skill # Save the ...
🌐
Programiz
programiz.com › python-programming › json
Python JSON: Read, Write, Parse JSON (With Examples)
Here, we have used the open() function to read the json file. Then, the file is parsed using json.load() method which gives us a dictionary named data. If you do not know how to read and write files in Python, we recommend you to check Python File I/O.
Find elsewhere
🌐
Medium
medium.com › data-science › working-with-json-data-in-python-45e25ff958ce
JSON in Python Tutorial | TDS Archive
August 11, 2021 - Writing to json files, reading from json files explained and illustrated with examples in python.
🌐
SitePoint
sitepoint.com › blog › programming › working with json files in python, with examples
Working with JSON Files in Python, with Examples — SitePoint
November 7, 2024 - We open mother.json in write mode. Since there’s no such file, one is created for us. The json.dump() method encodes the Python dictionary assigned to the mother variable to a JSON equivalent, which is written into the specified file.
🌐
W3Schools
w3schools.com › python › gloss_python_format_json.asp
Python Format JSON
Python Examples Python Compiler Python Exercises Python Quiz Python Challenges Python Server Python Syllabus Python Study Plan Python Interview Q&A Python Bootcamp Python Certificate Python Training ... The example above prints a JSON string, but it is not very easy to read, with no indentations and line breaks.
🌐
Reddit
reddit.com › r/learnpython › formatting json output in python
r/learnpython on Reddit: formatting json output in Python
May 12, 2022 -

Hi,

I would like to read json into Python code, and then output processed json. In order to get started with this, I have written very basic Python, and am attempting to read in very basic json I found online.

The input json is:

{
    "firstName": "John",
    "lastName": "Doe",
    "hobbies": ["biking", "coding", "rapping"],
    "age": 35,
    "children": [
        {
            "firstName": "hector",
            "age": 6
        },
        {
            "firstName": "cassandra",
            "age": 8
        }
    ]
}

The code is:

import json

if __name__ == '__main__':
    
    print( "start" )

    # read and load input json
    json_input_filename = "input.json"
    json_input = open( json_input_filename )

    json_input_dict = json.load( json_input )

    # write output json
    json_output_filename = "output.json"
    with open( json_output_filename, 'w' ) as json_output:
        json.dump( json_string, json_output )
  

    print( f"end" )

and the output is:

"{\"firstName\": \"John\", \"lastName\": \"Doe\", \"hobbies\": [\"biking\", \"coding\", \"rapping\"], \"age\": 35, \"children\": [{\"firstName\": \"hector\", \"age\": 6}, {\"firstName\": \"cassandra\", \"age\": 8}]}"

What can I do in order to preserve something resembling the original formatting? I'm going to load this output into some other code in order to process it further.

Thank you very much

🌐
GeeksforGeeks
geeksforgeeks.org › python › read-json-file-using-python
Read JSON file using Python - GeeksforGeeks
Error: Failed to decode JSON from the file. The Deserialization of JSON means the conversion of JSON objects into their respective Python objects. The load()/loads() method is used for it.
Published   September 15, 2025
Top answer
1 of 16
3349

data is a Python dictionary. It needs to be encoded as JSON before writing.

Use this for maximum compatibility (Python 2 and 3):

import json
with open('data.json', 'w') as f:
    json.dump(data, f)

On a modern system (i.e. Python 3 and UTF-8 support), you can write a nicer file using:

import json
with open('data.json', 'w', encoding='utf-8') as f:
    json.dump(data, f, ensure_ascii=False, indent=4)

See json documentation.

2 of 16
347

To get utf8-encoded file as opposed to ascii-encoded in the accepted answer for Python 2 use:

import io, json
with io.open('data.txt', 'w', encoding='utf-8') as f:
  f.write(json.dumps(data, ensure_ascii=False))

The code is simpler in Python 3:

import json
with open('data.txt', 'w') as f:
  json.dump(data, f, ensure_ascii=False)

On Windows, the encoding='utf-8' argument to open is still necessary.

To avoid storing an encoded copy of the data in memory (result of dumps) and to output utf8-encoded bytestrings in both Python 2 and 3, use:

import json, codecs
with open('data.txt', 'wb') as f:
    json.dump(data, codecs.getwriter('utf-8')(f), ensure_ascii=False)

The codecs.getwriter call is redundant in Python 3 but required for Python 2


Readability and size:

The use of ensure_ascii=False gives better readability and smaller size:

>>> json.dumps({'price': '€10'})
'{"price": "\\u20ac10"}'
>>> json.dumps({'price': '€10'}, ensure_ascii=False)
'{"price": "€10"}'

>>> len(json.dumps({'абвгд': 1}))
37
>>> len(json.dumps({'абвгд': 1}, ensure_ascii=False).encode('utf8'))
17

Further improve readability by adding flags indent=4, sort_keys=True (as suggested by dinos66) to arguments of dump or dumps. This way you'll get a nicely indented sorted structure in the json file at the cost of a slightly larger file size.

🌐
Stack Abuse
stackabuse.com › reading-and-writing-json-to-a-file-in-python
Reading and Writing JSON to a File in Python
April 18, 2023 - In this guide, we'll take a look at how to read and write JSON data from and to a file in Python, using the json module.
🌐
Zyte
zyte.com › home › blog › json parsing with python [practical guide]
A Practical Guide to JSON Parsing with Python
July 6, 2023 - In this guide, we’ll explore the syntax and data types of JSON, as well as the Python libraries and methods used for parsing JSON data, including more advanced options like JMESPath and ChompJS, which are very useful for web scraping data. One of the most common tasks when working with JSON data is to read its contents. Python provides several built-in libraries for reading JSON from files...
🌐
freeCodeCamp
freecodecamp.org › news › loading-a-json-file-in-python-how-to-read-and-parse-json
Loading a JSON File in Python – How to Read and Parse JSON
July 25, 2022 - JSON is short for JavaScript Object Notation. It's a simple syntax for storing data in name-value pairs. Values can be different data types as long as they are valid. Non-acceptable types for JSON include functions, dates, and undefined.
🌐
Board Infinity
boardinfinity.com › blog › json-file-in-python
JSON file in Python: Read and Write | Board Infinity
January 3, 2025 - Learn how to work with JSON files in Python and understand how to read, write, and use a JSON file in Python using the JSON module.
🌐
Simplilearn
simplilearn.com › home › resources › software development › json python: read, write, and parse json files in python
JSON Python: Read, Write, and Parse JSON Files in Python
October 11, 2022 - JSON, short for JavaScript object notation, is a syntax for storing and exchanging data. Learn how to write JSON to a file and convert from python to JSON now!
Address   5851 Legacy Circle, 6th Floor, Plano, TX 75024 United States
🌐
Canard Analytics
canardanalytics.com › blog › json-files-in-python
Working with JSON Files in Python | Canard Analytics
June 28, 2023 - The json.load() method takes a file path as a required attribute and returns a Python dictionary where the keys in the JSON file correspond to the keys in the dictionary, and values in the JSON file are converted to values in the dictionary according to the conversion table above.