JSON to convert python object to JSON object and pprint to implement pretty print. In this source code, we have imported two modules.In the using pprint() module we have printed the data in a pretty print format. In our example, we have created a python dictionary and then converted that python dictionary to JSON object.Regular json string could be in one line without any indentations. Using pprint() method in python we can improve the presentation of the JSON string.So please don’t get confused, simple json file can be called as json string. JSON file look like a python dictionary but it has a ‘str’ datatype which means its a string.In this section, we will learn about python pretty print JSON string. Read: Python Screen Capture Python Pretty Print JSON String In this output, when we run the code, the information is fetched from the website and converted to a JSON object and a new file is created. Pretty = json.dumps(response.json(), indent=2) In the end, using write() method we write the data into the file.to store the result in a file we have used python open() method.Also, dumped data appears like a pretty print (pprint) method.
We have used json.dump() to convert the data into json object.Using requests module we have fetched the JSON data from the URL.In our example, we have used sample JSON data that is available on boredapi website.pprint() method prints the json data then and there in an organised manner whereas dump() methods converts the python object to json object data can be written to file.Please note that json.dump() method is used to convert python object to json object and the output appears in an organised manner like pprint() method do.Pretty Print (pprint) module offers wide range of modules that helps in cleaning the data and present it more nicely.PPrint is a built-in module in python that can be imported directly without any installation.In this section, we will learn about Python pretty print JSON to file conversion. Python Pretty Print JSON One Liner Python Pretty Print JSON to File