Q:

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

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)

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now