'Formating python json dump
I am writing a python script that will parse through a directory of over 100 images and dump the information into a JSON file. The script is able to properly update and append data, but I want to improve the formmating. I want it to be compact like this:
{
"list_example" : [1, 2, 3, 4, 5, 6, 7, 8, 9],
"dict_example": {
"index1": {
"image_label_1": "image_file_path_1"
},
"index2": {
"image_label_2": "image_file_path_2"
}
}
}
The current JSON file looks like this:
{
"list_example": [
1,
2,
3,
4,
5,
6,
7,
8,
9
],
"dict_example": {
"index": {
"label": "image_file_path_1"
},
"index2": {
"label2": "image_file_path_2"
}
}
}
I am struggling to find a way to keep the array contained in one line while having the dictionaries on separate lines. This is the current code I am using to dump the data:
# Write to json file
with open('dataset.json','w') as outfile:
json.dump(example_data, outfile, indent=3)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
