
- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Check whether a string is valid JSON or not in Python
JSON is a type of text format use to exchange data easily between various computer programs. It has a specific format which Python can validate. In this article we will consider a string and using JSON module we will validate if the string represents a valid JSON format or not.
Creating JSON Object
The json module has method called loads. It loads a valid json string to create a Json object. In this example we load the string and check that there is no error in loading the JSON object. If there is error we consider the JSON string as invalid.
Example
import json Astring= '{"Mon" : "2pm", "Wed" : "9pm" ,"Fri" : "6pm"}' # Given string print("Given string", Astring) # Validate JSON try: json_obj = json.loads(Astring) print("A valid JSON") except ValueError as e: print("Not a valid JSON") # Checking again Astring= '{"Mon" : 2pm, "Wed" : "9pm" ,"Fri" : "6pm"}' # Given string print("Given string", Astring) # Validate JSON try: json_obj = json.loads(Astring) print("A valid JSON") except ValueError as e: print("Not a valid JSON") # Nested levels Astring = '{ "Timetable": {"Mon" : "2pm", "Wed" : "9pm"}}' # Given string print("Given string", Astring) # Validate JSON try: json_obj = json.loads(Astring) print("A valid JSON") except ValueError as e: print("Not a valid JSON")
Output
Running the above code gives us the following result −
Given string {"Mon" : "2pm", "Wed" : "9pm" ,"Fri" : "6pm"} A valid JSON Given string {"Mon" : 2pm, "Wed" : "9pm" ,"Fri" : "6pm"} Not a valid JSON Given string { "Timetable": {"Mon" : "2pm", "Wed" : "9pm"}} A valid JSON
- Related Questions & Answers
- Check whether triangle is valid or not if sides are given in Python
- Program to check whether a board is valid N queens solution or not in python
- Program to check whether given list is in valid state or not in Python
- Check whether right angled triangle is valid or not for large sides in Python
- Check whether the given string is a valid identifier in Python
- How to Check Whether a String is Palindrome or Not using Python?
- Python program to check whether a given string is Heterogram or not
- Python Program to Check Whether a String is a Palindrome or not Using Recursion
- C# program to check whether a given string is Heterogram or not
- Java program to check whether a given string is Heterogram or not
- Python program to check credit card number is valid or not
- Python program to check whether a list is empty or not?
- C++ program to check whether given string is bad or not
- Check whether N is a Dihedral Prime Number or not in Python
- Check whether the Average Character of the String is present or not in Python
Advertisements