The object contains strings with JSON encoding. Each element in the list referenced by the list key is a string object, that happens to hold valid JSON data. By using json.dumps() you encoded those strings to JSON values, so the use of backslashes is entirely correct; it is opaque data otherwise, it doesn't matter to the JSON encoder that the data is itself also valid JSON.

That those elements are JSON strings is probably an error on the side of the service you called. You now need to undo that mistake by decoding those JSON strings first:

response = client.get_products()
response['list'] = [json.loads(s) for s in response['list']]
with open('file.json', 'w') as output:
    json.dump(response, output)

If you also are responsible for the API output, fix that output. Don't double-encode your data.

As you discovered, using str() does not produce JSON output. That produces a Python representation, using valid Python syntax.

Answer from Martijn Pieters on Stack Overflow
๐ŸŒ
W3Schools
w3schools.com โ€บ python โ€บ gloss_python_format_json.asp
Python Format JSON
json.dumps(x, indent=4, separators=(". ", " = ")) Try it Yourself ยป ยท Python JSON Tutorial JSON Parse JSON Convert into JSON Sort JSON
๐ŸŒ
W3Schools
w3schools.in โ€บ python โ€บ json
Learn How to Work with JSON Data in Python - W3Schools
This tutorial explains using Python to work with JSON data by parsing JSON strings, converting Python objects to JSON, and performing standard JSON operations. Python offers excellent support for JSON, enabling you to parse, generate, and manipulate JSON data easily.
Discussions

Format of a JSON response with Python - Stack Overflow
I've been working on a side-project and I've been struggling with extracting data from a JSON response using Python. Whatever I come up with, I can't seem to have a proper formatted JSON result (pr... More on stackoverflow.com
๐ŸŒ stackoverflow.com
python - How to prettyprint a JSON file? - Stack Overflow
Aside from that, it's valid for a JSON document to represent a single string. Determining which processing to use with the input should be the programmer's responsiblity, from applying logical reasoning - while Python is designed to allow this kind of flexibility, doing explicit type checking ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
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 - What's the best way to parse a JSON response from the requests library? - Stack Overflow
I'm using the python requests module to send a RESTful GET to a server, for which I get a response in JSON. The JSON response is basically just a list of lists. What's the best way to coerce the More on stackoverflow.com
๐ŸŒ stackoverflow.com
๐ŸŒ
W3Schools
w3schools.com โ€บ python โ€บ python_json.asp
Python JSON
If you have a JSON string, you can parse it by using the json.loads() method. The result will be a Python dictionary.
๐ŸŒ
W3Schools
w3schools.com โ€บ python โ€บ gloss_python_json_parse.asp
Python JSON Parse
Python JSON Tutorial JSON Convert into JSON Format JSON Sort JSON
๐ŸŒ
W3Schools
w3schools.com โ€บ python โ€บ gloss_python_convert_into_JSON.asp
Python Convert From Python to JSON
If you have a Python object, you can convert it into a JSON string by using the json.dumps() method.
Top answer
1 of 15
3098

Use the indent= parameter of json.dump() or json.dumps() to specify how many spaces to indent by:

>>> import json
>>> your_json = '["foo", {"bar": ["baz", null, 1.0, 2]}]'
>>> parsed = json.loads(your_json)
>>> print(json.dumps(parsed, indent=4))
[
    "foo",
    {
        "bar": [
            "baz",
            null,
            1.0,
            2
        ]
    }
]

To parse a file, use json.load():

with open('filename.txt', 'r') as handle:
    parsed = json.load(handle)
2 of 15
500

You can do this on the command line:

python3 -m json.tool some.json

(as already mentioned in the commentaries to the question, thanks to @Kai Petzke for the python3 suggestion).

Actually python is not my favourite tool as far as json processing on the command line is concerned. For simple pretty printing is ok, but if you want to manipulate the json it can become overcomplicated. You'd soon need to write a separate script-file, you could end up with maps whose keys are u"some-key" (python unicode), which makes selecting fields more difficult and doesn't really go in the direction of pretty-printing.

You can also use jq:

jq . some.json

and you get colors as a bonus (and way easier extendability).

Addendum: There is some confusion in the comments about using jq to process large JSON files on the one hand, and having a very large jq program on the other. For pretty-printing a file consisting of a single large JSON entity, the practical limitation is RAM. For pretty-printing a 2GB file consisting of a single array of real-world data, the "maximum resident set size" required for pretty-printing was 5GB (whether using jq 1.5 or 1.6). Note also that jq can be used from within python after pip install jq.

Find elsewhere
๐ŸŒ
Real Python
realpython.com โ€บ python-json
Working With JSON Data in Python โ€“ Real Python
August 20, 2025 - Both XML and YAML serve similar purposes. If youโ€™re interested in how the formats differ, then you can check out the tutorial on how to serialize your data with Python. Free Bonus: Click here to download the free sample code that shows you how to work with JSON data in Python.
๐ŸŒ
DigitalOcean
digitalocean.com โ€บ community โ€บ tutorials โ€บ python-pretty-print-json
How to Pretty Print JSON in Python | DigitalOcean
September 16, 2025 - With Pythonโ€™s json.dumps() and pprint modules, you can quickly format output for better clarity. You can also use advanced parameters to handle non-ASCII characters, serialize custom Python objects, and fine-tune whitespace for compact output. This extends beyond simple output, proving valuable for debugging API responses, logging structured data, and improving readability of config files.
๐ŸŒ
ReqBin
reqbin.com โ€บ req โ€บ python โ€บ 4gvqbdi1 โ€บ json-response-format-example
Python | What is the correct JSON Response Format?
December 23, 2022 - In this JSON Response Format example, we send a request to the ReqBin echo URL to get JSON Response from the server. Click Send to execute the JSON Response Format example online and see the results. The Python code was automatically generated for the JSON Response Format example.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ response-json-python-requests
response.json() - Python requests - GeeksforGeeks
July 12, 2025 - When we print the response it prints '<Response [200]>' which is the HTTP code that indicates success. To print the JSON data fetched we have used json() method which prints the JSON data in the Python dictionary format as seen in the output.
๐ŸŒ
W3Schools
w3schools.com โ€บ python โ€บ python_challenges_json.asp
Python JSON Code Challenge
Test your understanding of Python json by completing a small coding challenge.
๐ŸŒ
ReqBin
reqbin.com โ€บ code โ€บ python โ€บ 0l6wsqxp โ€บ python-pretty-print-json-example
How do I pretty print JSON in Python?
Click Execute to run the Python Pretty Print JSON example online and see result. ... import json ugly_json = '[ {"Customer": 1, "name": "Alice", "country": ["Spain", "Madrid"]}, \ {"Customer": 2, "name": "Jack", "country": ["UK", "London"]} ]' parsed_json = json.loads(ugly_json) pretty_json = json.dumps(parsed_json, indent=2) print(pretty_json) ... JavaScript Object Notation (JSON) is a language-independent text format for storing and exchanging data.
๐ŸŒ
Python
docs.python.org โ€บ 3 โ€บ library โ€บ json.html
JSON encoder and decoder โ€” Python 3.14.3 documentation
1 month ago - Serialize obj as a JSON formatted stream to fp (a .write()-supporting file-like object) using this Python-to-JSON conversion table.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ python-pretty-print-json
Python - Pretty Print JSON - GeeksforGeeks
July 23, 2025 - This code reads JSON data from a file called "test.json," parses it into a Python data structure, and then prints it using both the built-in print function and the pprint module. The pprint module is used to pretty-print the JSON data with specific formatting options like an indentation of 2, a line width of 30 characters, and compact representation.
๐ŸŒ
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

๐ŸŒ
freeCodeCamp
freecodecamp.org โ€บ news โ€บ how-to-pretty-print-json-in-python
How to Pretty Print JSON in Python
April 14, 2023 - We will also cover best practices used to pretty print JSON, and also talk about it's use cases. In Python, "pretty print" refers to formatting and presenting data structures such as lists, dictionaries, and tuples in a more readable and organized way.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ json-formatting-python
JSON Formatting in Python - GeeksforGeeks
May 14, 2021 - For example, if you are trying to build an exciting project like this, you need to format the JSON output to render the necessary results. So let's dive into the JSON module which Python offers for formatting JSON output.
๐ŸŒ
W3Schools
w3schools.com โ€บ python โ€บ ref_requests_response.asp
Python requests.Response Object
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 requests.Response() Object contains the server's response to the HTTP request.