Python | Check if string is valid JSON or not

Code # 1:

Exit :

 initial string {`akshat`: 1,` nikhil`: 2} Is valid json? false 

Code # 2:

# Python code for demonstration
# checking the string
# is JSON valid or not

  

import json

 

ini_string = "{` akshat` : 1, `nikhil`: 2}"

 
# print start line ini_string

print ( "initial string" , ini_string)

 
# line validation

try :

json_object = json.loads (ini_string)

  print ( " Is valid json? true " )

except ValueError as e :

print ( "Is valid json? false" )

# Python code for demonstration
# checking the string
# is JSON valid or not

  

import json

 
# ini_string = & # 39; {& quot; Geek & quot ;: 1, & quot; forGeeks & quot ;: 2} & # 39;

 

a = `{" name ":" John "," age ": 31," Salary ": 25000}`

b = `{" Subjects ": {"Maths": 85, "Physics": 90}} `

  
#printing initial ini_string

print ( "initial strings given -" , a, "" , b)

 
# line check

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 " )

Exit:

 initial strings given - {"name": "John", "age": 31, "Salary": 25000} {"Subjects": {"Maths": 85, "Physics": 90}} Is valid json? true