[982] Print and save a JSON file with indentation in Python
Certainly! To print a JSON file with indentation in Python, you can use the json
module. Here’s an example of how to do it:
import json
# Your JSON data (replace this with your actual data)
data = {
"name": "John",
"age": 30,
"city": "New York"
}
# Convert the data to a formatted JSON string with indentation
formatted_json = json.dumps(data, indent=4)
# Print the formatted JSON
print(formatted_json)
In this example:
- Replace the
data
dictionary with your actual JSON data. - The
indent=4
argument injson.dumps()
specifies the number of spaces for indentation (you can adjust it as needed).
When you run this code, it will print the JSON data with proper indentation. Feel free to replace the sample data with your own JSON content! 🐍📄
Example:
import json
dic = '{"type": "FeatureCollection", "crs": {"type": "name", "properties": {"name": "urn:ogc:def:crs:EPSG::4283"}}, "features": [{"type": "Feature", "properties": {"Address": "Test, Property Address, 2000", "LotDP": "no lot / dp not given"}, "geometry": {"type": "Polygon", "coordinates": [[[152.26222229, -27.5630016919999], [152.261580527, -27.566587679], [152.25976944, -27.566330239], [152.260747395, -27.5608659609999], [152.26122243, -27.5611017049999], [152.261782341, -27.5615065539999], [152.262388274, -27.5620741769999], [152.26222229, -27.5630016919999]]]}}]}'
print(json.dumps(json.loads(dic), indent=4))
Output:
{
"type": "FeatureCollection",
"crs": {
"type": "name",
"properties": {
"name": "urn:ogc:def:crs:EPSG::4283"
}
},
"features": [
{
"type": "Feature",
"properties": {
"Address": "Test, Property Address, 2000",
"LotDP": "no lot / dp not given"
},
"geometry": {
"type": "Polygon",
"coordinates": [
[
[
152.26222229,
-27.5630016919999
],
[
152.261580527,
-27.566587679
],
[
152.25976944,
-27.566330239
],
[
152.260747395,
-27.5608659609999
],
[
152.26122243,
-27.5611017049999
],
[
152.261782341,
-27.5615065539999
],
[
152.262388274,
-27.5620741769999
],
[
152.26222229,
-27.5630016919999
]
]
]
}
}
]
}
{"name": "John", "age": 30, "city": "New York"}
{'name': 'John', 'age': 30, 'city': 'New York'}
{
"name": "John",
"age": 30,
"city": "New York"
}
Certainly! To save a Python dictionary to a JSON file with indentation, you can use the json
module. Here’s an example of how to do it:
import json
# Sample dictionary
my_dict = {
"name": "John",
"age": 30,
"city": "New York"
}
# Specify the file path where you want to save the JSON data
output_file = "my_dict.json"
# Write the dictionary to the JSON file with indentation (4 spaces)
with open(output_file, "w") as json_file:
json.dump(my_dict, json_file, indent=4)
print(f"Saved dictionary to {output_file} with indentation.")
In this example:
- Replace
my_dict
with your actual dictionary. - Adjust the
output_file
variable to the desired file path. - The
indent=4
argument ensures that the JSON data is formatted with 4 spaces for readability.
After running this code, you’ll have a JSON file (my_dict.json
) containing the dictionary data with proper indentation. 📝🐍