banner



How To Dump The Data Pprint Using Python

The pprint module adds a capability to "pretty-print" arbitrary Python data structures in a form that can be used every bit input to the interpreter. In this tutorial, volition learn near Python Pretty Print JSON. Also, nosotros volition comprehend these topics.

  • Python Pretty Print JSON to File
  • Python Pretty Print JSON Cord
  • Python Pretty Print JSON Command Line
  • Python Pretty Print JSON Response
  • Python Pretty Print JSON Object
  • Python Pretty Print JSON Dict
  • Python Pretty Print JSON color
  • Python Pretty Print JSON Dump
  • Pytho Pretty Print JSON One Liner

Python Pretty Print JSON to File

In this section, we will acquire about Python pretty print JSON to file conversion.

  • PPrint is a born module in python that can exist imported direct without any installation.
  • Pretty Print (pprint) module offers wide range of modules that helps in cleaning the information and present it more nicely.
  • 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 exercise. But they both are not same.
  • pprint() method prints the json data and so and at that place in an organised manner whereas dump() methods converts the python object to json object information can be written to file.
  • In our example, nosotros have used sample JSON data that is available on boredapi website.

Source Code:

  • Using requests module we have fetched the JSON data from the URL. This URL is from th boredapi.
  • Nosotros have used json.dump() to convert the data into json object. Also, dumped data appears similar a pretty print (pprint) method.
  • to shop the result in a file we take used python open() method.
  • In the finish, using write() method nosotros write the information into the file.
          import requests import json  response = requests.go("https://www.boredapi.com/api/activity") pretty = json.dumps(response.json(), indent=ii) res = open up('issue.json', mode='w') res.write(pretty)                  

Output:

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.

python pretty print json to file
Python Pretty Print JSON to File

Read: Python Screen Capture

Python Pretty Print JSON String

In this section, we will learn about python pretty print JSON string.

  • JSON file await like a python dictionary but it has a 'str' datatype which means its a cord. So delight don't get confused, simple json file can be chosen equally json string.
  • Using pprint() method in python we tin can improve the presentation of the JSON cord. Regular json string could exist in one line without any indentations.
  • In our example, we have created a python dictionary and then converted that python lexicon to JSON object. In the using pprint() module we have printed the data in a pretty print format.

Source Code:

  • In this source code, we have imported two modules. JSON to convert python object to JSON object and pprint to implement pretty impress.
  • Using json.loads() method python object is converted to JSON object.
  • In the end, we take used pprint() method from python pprint module using which we can impress the outcome in a prettly format.
          import json import pprint  json_data = '[{"id": 1, "name": "Andrew West Benton", "gender": "male", "race": "Asian", "metropolis": "BELLFLOWER"},'\ '{"id": 2, "proper noun": "Carlena E Cleek", "gender": "female person", "race": "white", "urban center": "PEMAQUID"}]'  json_object = json.loads(json_data) pprint.pprint(json_object)                  

Output:

In this output, the python dictionary object is displayed in JSON format with proper indentation practical using the pretty print method.

python-pretty print JSON string
Python Pretty Print JSON String

Read: Python print without newline

Python Pretty Print JSON Command Line

In this section, we will learn how to run python pretty impress JSON on the command line.

Suppose you take a JSON file on your machine and you desire to view it in a pretty impress format and then you can do that using the following command.

          cat file.json | python3 -m json.tool        
  • Here, cat is the linux control use to view the file data. cat file.json this command will display the entire content of file.json on the terminal.
  • python3 -m json.tool this command will assist in displaying the file.json data in a pretty print format.
  • If y'all are windows user and then use python instead of python3 and use 'echo' inplace of cat.

Output:

In this output, you tin see that nosotros accept run the pretty impress JSON on a last.

python pretty print json command line
Python Pretty JSON Command Line

Read: Python impress 2 decimal places

Python Pretty Print JSON Response

In the section, nosotros will acquire about Python Pretty Print JSON Response.

  • Response and Request are the two terms that works back and through.
  • Everytime we make request from a website or URL, a response is received. No response means website is non working.
  • Using Python Requests module we tin can become the information from the URL. This information is chosen Response. If no response is received that ways wrong url is passed.
  • Once nosotros have collected the response we can but dump the JSON data or we can employ pretty impress method to make it expect make clean.
  • Plese note that dump() and pretty impress (pprint) method do the same things hither just they are not aforementioned.

Source Code:

In this lawmaking, the requests module is used to fetch the response from the URL and so the data is converted to a JSON object and and then presented using a impress method of the python print module.

          import requests import json import pprint  response = requests.get("https://www.boredapi.com/api/activity") pretty = json.dumps(response.json(), indent=2) pprint.pprint(pretty)        

Output:

In this output, you can see that JSON information is represented in a proper format using pretty print.

python pretty print json response
Python Pretty Print JSON Response

Read: How to print factorial of a number in Python

Python Pretty Print JSON Object

In this department, we volition learn about the Python Pretty Print JSON object. Using json.loads() method of JSON module nosotros can create the JSON object.

In the below code first, nosotros have converted the python dictionary object to a JSON object by using json.loads() method then we have printed the object using pprint() method of pprint module.

          import json import pprint  json_data = '[{"id": i, "proper name": "Andrew W Benton", "gender": "male", "race": "Asian", "metropolis": "BELLFLOWER"},'\ '{"id": 2, "proper noun": "Carlena E Cleek", "gender": "female person", "race": "white", "city": "PEMAQUID"}]'  json_object = json.loads(json_data) pprint.pprint(json_object)        

Output:

In this output, a python pretty print JSON object is displayed.

python-pretty print JSON string
Python Pretty Print JSON Object

Read: How to Print Python Fibonacci series

Python Pretty Impress JSON Dict

In this department, we will learn almost Python Pretty Print JSON Dict. This method tin can be used for Python pretty print JSON array as well.

  • JSON and Python Dictionary look like but both take different data types.
  • JSON object has string datatype ('str') and dictionary has 'dict' datatype.
  • Using json.loads() method we tin can convert the python object to JSON object.
  • python pprint method is used to display the data in pretty impress format.
          import json import pprint  json_data = '[{"id": 1, "name": "Andrew W Benton", "gender": "male", "race": "Asian", "city": "BELLFLOWER"},'\ '{"id": two, "name": "Carlena East Cleek", "gender": "female person", "race": "white", "urban center": "PEMAQUID"}]'  json_object = json.loads(json_data) pprint.pprint(json_object)        

Output:

In this output, data is displayed in a tree structure using pprint() method of the python pprint module.

python-pretty print JSON string
Python Pretty Print JSON Dict

Source Code:

In this source code, json_data is a dictionary and we have converted it to JSON using json.load() method and and so we have used pprint() to print the JSON data in a Pretty Print format.

          import json import pprint  json_data = {"id": i, "proper name": "Andrew W Benton", "gender": "male", "race": "Asian", "urban center": "BELLFLOWER", "mobile": "217-412-6676"}  print(type(json_data))  d = json.load(json_data) pprint.pprint(d)        

Output:

In this output, nosotros have demonstrated python pretty print JSON dictionary. Nosotros have converted the dictionary data to Python Pretty Print JSON.

python pretty print json dict
Python Pretty Print JSON Dict

Read: Python program to print prime numbers

Python Pretty Impress JSON color

In this section, nosotros will learn about Python Pretty Print JSON colour.

  • Using Python Rich module we tin color the pretty print JSON on the final. It uses the color on final to make the JSON look nice.
  • Python RICH module provide print_json() method. Anything printed using this method appears colorful on the terminal.
  • Using Python Request module data is fetched from the website. In our case we are using boredapi, it the sample json available on the internet.
  • json.dumps() method converts the normal json to pretty json file which is organised with indentation and spaces.

Source Lawmaking:

In this source lawmaking, we have used the python requests module to fetch the data from the URL. The print_json() method of the rich module is used to color the output on the terminal.

          import requests import json from rich import print_json  response = requests.go("https://www.boredapi.com/api/activeness") pretty = json.dumps(response.json(), indent=4)  impress(pretty) print_json(pretty)        

Output:

In this output, python pretty print JSON colour is displayed. To prove the distinction we take displayed the regular output first and and then colorful output.

Python Pretty Print JSON Color
Python Pretty Impress JSON Color

Read: Python program to print element in an array

Python Pretty Impress JSON Dumps

In this department, we will larn about Python Impress JSON Dumps.

  • Python JSON dumps method converts the python object to json object also it displays the data in an organised style. The information is seperated with indentation and looks very make clean.
  • Python JSON Dump purpose is to convert python to json object where as pprint() method provides wide variety of methods to control the look and feel of displayed data.
  • In our example, we have converted a python dictionary data to json data using json.dumps() method.

Source Code:

In this source lawmaking, the JSON module is imported and json.dumps() method is used to catechumen python data to JSON.

          import json  # raw json json_data = {"id": i, "name": "Andrew W Benton", "gender": "male", "race": "Asian", "metropolis": "BELLFLOWER", "mobile": "217-412-6676"}   # using dump to pretty json pretty_json = json.dumps(json_data, indent=2)  # printing issue print(pretty_json)        

Output:

In this output, y'all can encounter that the information is displayed in a proper format. It has indentation and spaces where always required. This form of JSON looks more than clean and like shooting fish in a barrel to understand.

python pretty print to json dump
Python Pretty Print JSON Dump

Read: Python program to print pattern

Python Pretty Print JSON 1 Liner

In this department, we will acquire near the Python Pretty Impress JSON one-liner. At times you have to run the JSON file without opening information technology into a text or code editor at that time this method can be used. Information technology is a one-line code to view the JSON file using Python.

          echo '{"Name":["john", "moly"],"Form":["A", "C"]}' | python -mjson.tool        

Yous may also like to read the following articles.

  • Python Count Words in File
  • Case statement in Python
  • Python print without newline
  • eleven Python list methods
  • Python Lexicon Methods + Examples

In this tutorial, we have learned about Python pretty print JSON. Besides, we have covered these topics.

  • Python Pretty Print JSON to File
  • Python Pretty Print JSON Cord
  • Python Pretty Print JSON Command Line
  • Python Pretty Print JSON Response
  • Python Pretty Print JSON Object
  • Python Pretty Print JSON Dict
  • Python Pretty Print JSON color
  • Python Pretty Impress JSON Dump
  • Python Pretty Print JSON 1 Liner

How To Dump The Data Pprint Using Python,

Source: https://pythonguides.com/python-pretty-print-json/

Posted by: martintrathem2001.blogspot.com

0 Response to "How To Dump The Data Pprint Using Python"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel