pretty-print json in python

pretty-print json in python

Hi,

JSON is very easy way of saving and sending the data over the internet. I have a web service and we are calling it in our Python program. Now the returned the JSON is not formatted so my requirement is to format it and use it.

How we can easily format a JSON in Python with minimal code? I would prefer using any existing library.

Here is the sample of JSON:

{"id":"0144", "client_name":"My client","country":"India"}

This is the sample JSON file, but my actually file contains over 100 tags. So, it becomes very difficult to read without formatting.

Thanks

View Answers

July 18, 2021 at 2:23 PM

Hi,

You can use the json library which is very robust and used by Python developers all over the world.

Here is the example of converting your json in pretty format.

import json

jsonObj = {"id":"0144", "client_name":"My client","country":"India"}

pretty_json = json.dumps(jsonObj, indent=4)  
print(pretty_json)

Above example will print the json in following pretty format:

>>> pretty_json = json.dumps(jsonObj, indent=4)  
>>> 
>>> 
>>> print(pretty_json) 
{
    "id": "0144",
    "client_name": "My client",
    "country": "India"
}
>>>

This way you can format JSON in Python program.

Thanks


July 18, 2021 at 2:24 PM

Hi,

You may this also:

How to pretty print a JSON file in Python?

Thanks


July 18, 2021 at 2:30 PM

Hi

If you have jsondata in string format then you may use following code:

import json

strJson = '{"id":"0144", "client_name":"My client","country":"India"}'

jsonObj = json.loads(strJson)
pretty_json = json.dumps(jsonObj, indent=4)  
print(pretty_json)

The jsonObj = json.loads(strJson) code will parse string json into JSON object. After that you can use the json.dumps() function to pretty print.

thanks









Related Tutorials/Questions & Answers:
pretty-print json in python
pretty-print json in python  Hi, JSON is very easy way of saving... it in our Python program. Now the returned the JSON is not formatted so my requirement is to format it and use it. How we can easily format a JSON in Python
ModuleNotFoundError: No module named 'pretty-print-json'
-print-json python with following command: pip install pretty-print-json After the installation of pretty-print-json python library, ModuleNotFoundError...ModuleNotFoundError: No module named 'pretty-print-json'  Hi, My
Advertisements
How to pretty print a JSON file in Python?
How to pretty print a JSON file in Python? In this tutorial I will teach you to read the JSON file in Python and then pretty print the content... in Python, format the JSON and print the formatted content. The formatted content can
ModuleNotFoundError: No module named 'pretty_print_dictionary'
install pretty_print_dictionary python with following command: pip install... python library, ModuleNotFoundError: No module named 'pretty_print...ModuleNotFoundError: No module named 'pretty_print_dictionary'  Hi
ModuleNotFoundError: No module named 'pretty-print-url'
python environment you have to install padas library. You can install pretty-print-url python with following command: pip install pretty-print-url After the installation of pretty-print-url python library, ModuleNotFoundError
How to pretty print XML from Java?
How to pretty print XML from Java?  How to pretty print XML from Java
How to convert Python dictionary to JSON?
How to convert Python dictionary to JSON? Python example to create dictionary and convert to JSON pretty print format These days JSON is very popular... json_object = json.dumps(week) print(json_object) #Get pretty format
ModuleNotFoundError: No module named 'python-to-json'
ModuleNotFoundError: No module named 'python-to-json'  Hi, My... named 'python-to-json' How to remove the ModuleNotFoundError: No module named 'python-to-json' error? Thanks   Hi, In your python
Convert dictionary to JSON Python
Converting dictionary object to JSON in Python program Dictionary is one... = {"name":"Python Book", "price":"100"} j = json.dumps(book_dict) print(j) If you run the above code it will print following data: {"name": "Python
Convert dictionary to JSON Python
Converting dictionary object to JSON in Python program Dictionary is one.... Here is example: import json book_dict = {"name":"Python Book", "price...() is used to convert dictionary object to JSON string in Python
Python extract data from JSON
Python extract data from JSON  Hi, I have a program that reads json... getting it into a string variable in Python. Here is my JSON sample: {"accounid... is to parse this JSON in my python program and get the values in variables. How I can
ModuleNotFoundError: No module named 'json_stable_stringify_python'
ModuleNotFoundError: No module named 'json_stable_stringify_python'  ...: No module named 'json_stable_stringify_python' How to remove the ModuleNotFoundError: No module named 'json_stable_stringify_python' error
ModuleNotFoundError: No module named 'parser-json-python'
ModuleNotFoundError: No module named 'parser-json-python'  Hi, My... named 'parser-json-python' How to remove the ModuleNotFoundError: No module named 'parser-json-python' error? Thanks   Hi, In your
ModuleNotFoundError: No module named 'parser-json-python'
ModuleNotFoundError: No module named 'parser-json-python'  Hi, My... named 'parser-json-python' How to remove the ModuleNotFoundError: No module named 'parser-json-python' error? Thanks   Hi, In your
ModuleNotFoundError: No module named 'python-json-config'
ModuleNotFoundError: No module named 'python-json-config'  Hi, My... named 'python-json-config' How to remove the ModuleNotFoundError: No module named 'python-json-config' error? Thanks   Hi, In your
ModuleNotFoundError: No module named 'Python-JSON-Formatter'
ModuleNotFoundError: No module named 'Python-JSON-Formatter'  Hi...: No module named 'Python-JSON-Formatter' How to remove the ModuleNotFoundError: No module named 'Python-JSON-Formatter' error? Thanks   Hi
ModuleNotFoundError: No module named 'python-json-logger'
ModuleNotFoundError: No module named 'python-json-logger'  Hi, My... named 'python-json-logger' How to remove the ModuleNotFoundError: No module named 'python-json-logger' error? Thanks   Hi, In your
ModuleNotFoundError: No module named 'python-logging-json-formatter'
ModuleNotFoundError: No module named 'python-logging-json-formatter'  ...: No module named 'python-logging-json-formatter' How to remove the ModuleNotFoundError: No module named 'python-logging-json-formatter' error
ModuleNotFoundError: No module named 'ctodd-python-lib-json'
ModuleNotFoundError: No module named 'ctodd-python-lib-json'  Hi...: No module named 'ctodd-python-lib-json' How to remove the ModuleNotFoundError: No module named 'ctodd-python-lib-json' error? Thanks   Hi
ModuleNotFoundError: No module named 'json_stable_stringify_python'
ModuleNotFoundError: No module named 'json_stable_stringify_python'  ...: No module named 'json_stable_stringify_python' How to remove the ModuleNotFoundError: No module named 'json_stable_stringify_python' error
How to dynamically build a JSON object with Python
, I want full proof method to make json dynamically with Python. How to do this? How to dynamically build a JSON object with Python? Thanks   Hi, You can use the Python json library for this. The json module of python provide
How to dynamically build a JSON object with Python
, I want full proof method to make json dynamically with Python. How to do this? How to dynamically build a JSON object with Python? Thanks   Hi, You can use the Python json library for this. The json module of python provide
requests post python json body encode utf-8
requests post python json body encode utf-8  Hi, I am posting data... json body encode utf-8 How to resolve this? I am using python requests api...) print(json_data) Program output: >>> import json >>> dataObj
requests post python json body encode utf-8
requests post python json body encode utf-8  Hi, I am posting data... json body encode utf-8 How to resolve this? I am using python requests api...) print(json_data) Program output: >>> import json >>> dataObj
how to parse json in python and get data
how to parse json in python and get data  Hi, I have to parse the JSON string and get the value in my Python program. for example I want to get... me in solving this. Thanks   Hi, You can use the Python json
Read JSON file into Python dictionary
How to Read JSON file into Python dictionary? This is quick guide for developers to read a json file into Python dictionary object. JSON is most proffered...; >>> print(data["name"]) Python Book >>> >>> print
Read JSON file into Python dictionary
How to Read JSON file into Python dictionary? This is quick guide for developers to read a json file into Python dictionary object. JSON is most proffered...': 'Python Book', 'price': '100'} >>> >>> print(data["name
How do I convert a dictionary to a JSON object in Python?
How do I convert a dictionary to a JSON object in Python?  Hi, I... is returning the dictionary object. Now our requirement is to convert it to JSON and save into Elasticsearch. We also have to save the json to a database
Python Tutorials
Python dictionary to JSON? How to pretty print a JSON file in Python... arguments Convert dictionary to JSON Python Read JSON...; these examples. Learn How to print dot in python? Print
json
json  how connect ajax with json
json
json  how to get data in json object from database
json
data is transferred and how it is encoded. While JSON is a tool used for describing values and objects by encode the content in a very specific manner. 2)JSON... in JavaScript.While AJAX uses JSON format to communicate between client
json
json   how to develop iphone web service application using json. Please help me by giving any link where I could find an example or any help will be anticipated
JSON
it using JSON   The given code parse the message in JSON in JavaScript. You can parse the message with JSON in JavaScript by using the method "String.parseJSON(filter)". It parses the JSON message to an string or object
JSon Tutorial
JSon Tutorial  What is JSon? Where i can find the JSon tutorial? Thanks!   JSon Tutorial
JSON with Java
JSON with Java  How to request Json From URI and to parse and to store the values in Java array
ModuleNotFoundError: No module named 'bqtools-json'
ModuleNotFoundError: No module named 'bqtools-json'  Hi, My Python...;bqtools-json' error? Thanks   Hi, In your python environment you have to install padas library. You can install bqtools-json python
ModuleNotFoundError: No module named 'compress-json'
'compress-json' error? Thanks   Hi, In your python environment you have to install padas library. You can install compress-json python... the installation of compress-json python library, ModuleNotFoundError: No module named '
ModuleNotFoundError: No module named 'dc-json'
ModuleNotFoundError: No module named 'dc-json'  Hi, My Python... to install padas library. You can install dc-json python with following command: pip install dc-json After the installation of dc-json python library
ModuleNotFoundError: No module named 'excel-to-json'
'excel-to-json' error? Thanks   Hi, In your python environment you have to install padas library. You can install excel-to-json python... the installation of excel-to-json python library, ModuleNotFoundError: No module named '
ModuleNotFoundError: No module named 'human-json'
ModuleNotFoundError: No module named 'human-json'  Hi, My Python...;human-json' error? Thanks   Hi, In your python environment you have to install padas library. You can install human-json python
ModuleNotFoundError: No module named 'js.jquery_json'
'js.jquery_json' error? Thanks   Hi, In your python... python with following command: pip install js.jquery_json After the installation of js.jquery_json python library, ModuleNotFoundError: No module named
ModuleNotFoundError: No module named 'json-api'
ModuleNotFoundError: No module named 'json-api'  Hi, My Python... to install padas library. You can install json-api python with following command: pip install json-api After the installation of json-api python
ModuleNotFoundError: No module named 'json-api'
ModuleNotFoundError: No module named 'json-api'  Hi, My Python... to install padas library. You can install json-api python with following command: pip install json-api After the installation of json-api python
ModuleNotFoundError: No module named 'json-checker'
ModuleNotFoundError: No module named 'json-checker'  Hi, My Python...;json-checker' error? Thanks   Hi, In your python environment you have to install padas library. You can install json-checker python
ModuleNotFoundError: No module named 'json-codegen'
ModuleNotFoundError: No module named 'json-codegen'  Hi, My Python...;json-codegen' error? Thanks   Hi, In your python environment you have to install padas library. You can install json-codegen python
ModuleNotFoundError: No module named 'json_compare'
ModuleNotFoundError: No module named 'json_compare'  Hi, My Python...;json_compare' error? Thanks   Hi, In your python environment you have to install padas library. You can install json_compare python
ModuleNotFoundError: No module named 'json-configparser'
python environment you have to install padas library. You can install json-configparser python with following command: pip install json-configparser After the installation of json-configparser python library, ModuleNotFoundError
ModuleNotFoundError: No module named 'JSON-Datetime'
'JSON-Datetime' error? Thanks   Hi, In your python environment you have to install padas library. You can install JSON-Datetime python... the installation of JSON-Datetime python library, ModuleNotFoundError: No module named '
ModuleNotFoundError: No module named 'json_environ'
ModuleNotFoundError: No module named 'json_environ'  Hi, My Python...;json_environ' error? Thanks   Hi, In your python environment you have to install padas library. You can install json_environ python

Ads