Python Script 3: Validate and format JSON string

Json validation with python

As per official JSON website, JSON is a light-weight data interchange format. It is easy for humans to read and write. It is easy for machines to parse and generate. It is based on a subset of the JavaScript Programming Language, Standard ECMA-262 3rd Edition – December 1999.

In this small article we will see how to validate and format the JSON string using python.

Format JSON string:

Parameter indent  decides the number of spaces to use for indentation.  Parameter sort_keys  decides whether the keys in formatted JSON should be in sorted order or not. Default value is False .


Format JSON file:

We can use same code to format and validate the JSON file. Place a .json file with below content in current working directory.

Now use this piece of code to format the json file.


Error in JSON validation:

Any error in JSON string will be reported with exact location in string.
For example, a comma is missing in below JSON string.




You can pretty-print the small JSON string directly at command terminal without using python script file. Use below line of code.



You can add this crome app to your browser to validate and beautify the JSON strings.

Hosting Django App on PythonAnyWhere server.

(Visited 301 times, 1 visits today)

You must read this :

2 thoughts on “Python Script 3: Validate and format JSON string”

Leave a Reply

Your email address will not be published. Required fields are marked *