The Wayback Machine - https://web.archive.org/web/20220322071125/https://www.geeksforgeeks.org/python-check-whether-a-string-is-valid-json-or-not/
Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Python | Check whether a string is valid json or not

  • Difficulty Level : Hard
  • Last Updated : 15 Mar, 2019

Given a string, the task is to check whether a string is valid json object or not. Let’s try to understand the problem using different examples.

Code #1 :




# Python code to demonstrate
# checking whether string
# is valid json or not
  
import json
  
ini_string = "{'akshat' : 1, 'nikhil' : 2}"
  
# printing initial ini_string
print ("initial string", ini_string)
  
# checking for string
try:
    json_object = json.loads(ini_string)
    print ("Is valid json? true")
except ValueError as e:
    print ("Is valid json? false")
Output:
initial string {'akshat' : 1, 'nikhil' : 2}
Is valid json? false

 
Code #2:




# Python code to demonstrate
# checking whether string
# is valid json or not
  
import json
  
#ini_string = '{"Geek" : 1,"forGeeks" : 2}'
  
a = '{"name":"John", "age":31, "Salary":25000}'
b = '{ "Subjects": {"Maths":85, "Physics":90}}'
  
#printing initial ini_string
print ("initial strings given - \n", a, "\n", b)
  
#checking for string
try:
    json_object1 = json.loads(a)
    json_object2 = json.loads(b)
    print ("Is valid json? true")
      
except ValueError as e:
    print ("Is valid json? false")
Output:
initial strings given - 
 {"name":"John", "age":31, "Salary":25000} 
 { "Subjects": {"Maths":85, "Physics":90}}
Is valid json? true

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!