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 FalseCode language: Python (python)

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.

Download HitXP Mobile App

Get it on Google Play