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
Code 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.

Content Protection by DMCA.com

Download HitXP Mobile App

Get it on Google Play
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

FacebookLike
InstagramFollow
PinterestFollow
RSS FeedSubscribe
Sound CloudFollow
TwitterFollow
YoutubeSubscribe

Latest Articles

The difference between Itihasa and Puranas

Documentation of ancient Indian history and the historical events of Indian civilization in the form of Itihasa and Puranas - Ramayana and Mahabharata.

Online Education should be interactive and innovative – Fun to Learn

For online education to succeed for smaller classses - it should be as interesting as cartoons. Online education cant succeed unless it is more interesting, interactive and innovative than offline schooling.

The Complete List of Dhatus – Sanskrit Root Words and their English meaning

A complete list of all dhatus (root words or verb roots) in Sanskrit dictionary and their meanings with corresponding IPA transliteration.

Sanskrit Lesson 4 – Word Creation Magic using Dhatus

Learn how easy it is to create new words in Sanskrit using root words called Dhatus. This simple process of Sanskrit grammar powers the entire language.

Latest Music Notations

Title Track BGM – Irul – Piano Notations

Piano, Keyboard, Violin, Flute notes, Guitar Tabs and Sheet Music of the Song Title Track BGM from the 2021 Malayalam movie Irul in Western and Indian Notations.

Ninnukori Varnam – Agni Natchathiram – Piano Notations

Piano, Keyboard, Violin, Flute notes, Guitar Tabs and Sheet Music of the Song Ninnukori Varnam from the 1988 Tamil movie Agni Natchathiram in Western and Indian Notations.

Raaja Raajathi – Agni Natchathiram – Piano Notations

Piano, Keyboard, Violin, Flute notes, Guitar Tabs and Sheet Music of the Song Raaja Raajathi from the 1988 Tamil movie Agni Natchathiram in Western and Indian Notations.

Keli Premigale – Yuga Purusha – Piano Notations

Piano, Keyboard, Violin, Flute notes, Guitar Tabs and Sheet Music of the Song Keli Premigale from the 1989 Kannada movie Yuga Purusha in Western and Indian Notations.
Content Protection by DMCA.com