Change language

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 

Python | Check if string is valid JSON or not Python functions: Questions

Python | Check if string is valid JSON or not String Variables: Questions

Shop

Best laptop for Fortnite

$

Best laptop for Excel

$

Best laptop for Solidworks

$

Best laptop for Roblox

$

Best computer for crypto mining

$

Best laptop for Sims 4

$

Best laptop for Zoom

$499

Best laptop for Minecraft

$590

Latest questions

NUMPYNUMPY

psycopg2: insert multiple rows with one query

12 answers

NUMPYNUMPY

How to convert Nonetype to int or string?

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Javascript Error: IPython is not defined in JupyterLab

12 answers

News

Wiki

Python OpenCV | cv2.putText () method

numpy.arctan2 () in Python

Python | os.path.realpath () method

Python OpenCV | cv2.circle () method

Python OpenCV cv2.cvtColor () method

Python - Move item to the end of the list

time.perf_counter () function in Python

Check if one list is a subset of another in Python

Python os.path.join () method