Home > Blockchain >  Dumps Not Write To JSON file Pretty Print I Already using "indent=4" I added JSON View
Dumps Not Write To JSON file Pretty Print I Already using "indent=4" I added JSON View

Time:06-30

[enter image description here][1]I'm trying to convert a dict that I can't serialize to string type and write it to a json file. However, when using the dumps method, pretty printing does not occur in the json file.

    data = ''
    for db_name in client.list_database_names():
            db = client[db_name]
            for coll_name in db.list_collection_names():
                data  = str("DATABASE NAME: {}, Collection:{}".format(db_name, coll_name))
                data = json.dumps(data, default=str)
                json.loads(data)
     return data


  [1]: https://i.stack.imgur.com/6IsHn.png

CodePudding user response:

To get pretty printing using json.dumps() you need to include a parameter like indent=4. See the docs here.

CodePudding user response:

You need to use the indent argument in json.dumps() to create the pretty effect.

with open('filename.json', 'w') as f:
    f.write(json.dumps(data, indent=4)
  • Related