Home Code How to check if JSON string is valid in Python?

How to check if JSON string is valid in Python?

If you want to check if a given JSON string is in valid JSON format or not, then simply call the function is_valid_json given below. It will return True if the JSON is valid, else will return False.

import json

#returns True if json_str is a valid JSON string, else returns False
def is_valid_json(self,json_str):
	try:
		json.loads(json_str)
		return True
	except Exception as ex:
		return False

In the above script, we try to parse the JSON string using the default python JSON parser. If the parser does not throw any exception while parsing, then the string is valid JSON. Otherwise, it is invalid JSON.

Content Protection by DMCA.com
Gurudevhttps://www.hitxp.com
Gurudev is the developer of Gurunudi AI Platform. This is his official website where he pens his thoughts on a wide range of topics, answers queries, shares resources and tools developed by him.

LEAVE A REPLY

Please enter your comment!
Please enter your name here

4,248FansLike
10FollowersFollow
153FollowersFollow
377FollowersFollow

Subscribe to HitXP Articles

To be updated with all the latest articles, offers and special announcements.

Latest Articles

An Efficient Strategy to fight Corona Virus

By setting up local police station level online delivery networks for essential items like grocery, medicines - the need for general public to come out of their homes can be removed.

Rising pollution can cause Pandemics deadlier than Corona Virus

Rising pollution can expose ancient killer microbes that may cause pandemics more deadlier than Corona. Melting Ice Sheets can resurrect killer agents of the past.

Corona Virus, SARS – Why are new viruses increasingly originating from China?

The reason why new strains of deadly viruses are emerging from China. Viruses like coronavirus and SARS should be stopped from evolving and spreading to prevent another global pandemic.

Evolution of life – primitive cells, complex organisms, intelligence, what next after humans?

Evolution of life from primitive life forms to complex multi cellular organisms to intelligent human species - the advent of AI raises a natural question - What next after human?
Content Protection by DMCA.com