Q:

Check whether following json is valid or invalid. If Invalid correct it

belongs to collection: Python OOP Exercises

0

Check whether following json is valid or invalid. If Invalid correct it

{ 
   "company":{ 
      "employee":{ 
         "name":"emma",
         "payble":{ 
            "salary":7000
            "bonus":800
         }
      }
   }
}

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Solution1:

Python provides The json.tool module to validate JSON objects from the command line. Run the following command.

Command: echo "JSON DATA" | python -m json.tool

echo { "company":{ "employee":{ "name":"emma", "payble":{ "salary":7000 "bonus":800} } } } | python -m json.tool

Output:

Expecting ',' delimiter: line 1 column 68 (char 67)

Just add ',' after "salary":7000 to solve the error.

Solution2:

import json

def validateJSON(jsonData):
    try:
        json.loads(jsonData)
    except ValueError as err:
        return False
    return True

InvalidJsonData = """{ "company":{ "employee":{ "name":"emma", "payble":{ "salary":7000 "bonus":800} } } }"""
isValid = validateJSON(InvalidJsonData)

print("Given JSON string is Valid", isValid)

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Parse the following JSON to get all the values of ... >>
<< Convert the following JSON into Vehicle Object usi...