I am developing a script in python 3.10 that reads the content of some JSON files and I need, for the time that I am developing, that the content of the json files appears formatted with their tabs so that it is easier for me to read and understand the structure , since there can be many different types of structures and based on a flag in the file name I must interpret them.
The original file looks like this:
{"Request":{"ErpCode":"026013/01","ErpNumberCreator":null,"Articles":[{"EAN":"0823007700180","ManufacturerCode":2,"ManufacturerName":"HELLA"}]},"Response":{"Data":[{"EAN":"0823007700180","ManufacturerCode":2,"ManufacturerName":"HELLA"}],"Success":true,"Message":"00:00:00.01"}}
Whereas the idea is that when you open the file in a text/code editor, you can see it like this:
{
"Request":
{
"ErpCode": "026013/01",
"ErpNumberCreator": null,
"Articles":
[
{
"EAN": "0823007700180",
"ManufacturerCode": 2,
"ManufacturerName": "HELLA"
}
]
},
"Response":
{
"Data":
[
{
"EAN": "0823007700180",
"ManufacturerCode": 2,
"ManufacturerName": "HELLA"
}
],
"Success": true,
"Message": "00:00:00.01"
}
}
The module
json
(which comes in the standard library) can read files as well as write them. And when writing it, you can specify certain parameters on how you want to format it.In your case, if the file is called
entrada.json
, you can do:Learn more about what you can do
json.dump()
in your section of the manual