Python Tutorial

Python Flow Control

Python Functions

Python Data Types

Python Date and Time

Python Files

Python String

Python List

Python Dictionary

Python Variable

Python Input/Output

Python Exceptions

Python Advanced

How to pretty print JSON file or string in Python

To pretty print a JSON file or string in Python, you can use the json module from the Python standard library. The json.dumps() function takes a Python object (usually a dictionary or a list) and returns a JSON string. You can use the indent parameter to control the indentation of the output.

Here's an example for pretty printing a JSON string:

import json

# JSON data as a Python dictionary
data = {
    "name": "John",
    "age": 30,
    "city": "New York"
}

# Convert the Python object to a JSON string with indentation
pretty_json = json.dumps(data, indent=4)

# Print the pretty JSON string
print(pretty_json)

If you have a JSON file and you want to pretty print its content, you can read the file, load the JSON content into a Python object using json.load(), and then use json.dumps() to pretty print the JSON data:

import json

# Read the JSON file
with open("data.json", "r") as json_file:
    data = json.load(json_file)

# Convert the Python object to a JSON string with indentation
pretty_json = json.dumps(data, indent=4)

# Print the pretty JSON string
print(pretty_json)

In this example, we use the with statement and the open() function to read the JSON file called "data.json". The json.load() function loads the JSON content from the file into a Python object. We then use the json.dumps() function with the indent parameter set to 4 to convert the Python object to a JSON string with indentation. Finally, we print the pretty JSON string.

  1. Using json.dumps() for pretty printing in Python:

    import json
    
    data = {'name': 'John', 'age': 30, 'city': 'New York'}
    formatted_json = json.dumps(data, indent=2)
    
    print(formatted_json)
    
  2. Pretty print JSON with indent in Python:

    import json
    
    data = {'name': 'Alice', 'age': 25, 'city': 'London'}
    formatted_json = json.dumps(data, indent=4)
    
    print(formatted_json)
    
  3. Print JSON data in a readable format in Python:

    import json
    
    data = {'fruit': 'apple', 'color': 'red', 'quantity': 5}
    formatted_json = json.dumps(data, indent=2, sort_keys=True)
    
    print(formatted_json)
    
  4. Python json.dump() with indent for formatting JSON:

    import json
    
    data = {'animal': 'lion', 'sound': 'roar'}
    with open('output.json', 'w') as file:
        json.dump(data, file, indent=2)
    
  5. How to make JSON output more readable in Python:

    import json
    
    data = {'vehicle': 'car', 'color': 'blue', 'speed': 'fast'}
    formatted_json = json.dumps(data, indent=2, separators=(',', ': '))
    
    print(formatted_json)
    
  6. JSON beautification in Python:

    import json
    
    data = {'subject': 'Python', 'topic': 'JSON formatting'}
    formatted_json = json.dumps(data, indent=2, ensure_ascii=False)
    
    print(formatted_json)
    
  7. Print formatted JSON string in Python:

    import json
    
    data = {'language': 'Python', 'version': '3.9'}
    formatted_json = json.dumps(data, indent=2, sort_keys=True)
    
    print(formatted_json)
    
  8. Using pprint module for pretty printing JSON in Python:

    import json
    from pprint import pprint
    
    data = {'country': 'Japan', 'capital': 'Tokyo'}
    pprint(data, indent=2)
    
  9. Create indented JSON output with json.dumps() in Python:

    import json
    
    data = {'planet': 'Earth', 'population': 7_800_000_000}
    formatted_json = json.dumps(data, indent=2)
    
    print(formatted_json)
    
  10. How to pretty print a JSON file in Python:

    import json
    
    with open('data.json', 'r') as file:
        data = json.load(file)
    
    formatted_json = json.dumps(data, indent=2)
    print(formatted_json)