My program gets a JSON file that has an information for service.
Before run the service program, I want to check if the JSON file is valide(Only check whether all necessary keys exist).
Below is the standard(necessary datas) JSON format for this program:
{
"service" : "Some Service Name"
"customer" : {
"lastName" : "Kim",
"firstName" : "Bingbong",
"age" : "99",
}
}
Now I am checking the JSON file validation like this:
import json
def is_valid(json_file):
json_data = json.load(open('data.json'))
if json_data.get('service') == None:
return False
if json_data.get('customer').get('lastName') == None:
return False
if json_data.get('customer').get('firstName') == None:
return False
if json_data.get('customer').get('age') == None:
return False
return True
Actually, the JSON standard format has more than 20 keys. Is there any other way to check the JSON format?
There are various ways to validate JSON as per the standard convention format. As we know, the json module provides two methods to parse JSON data using Python. json.loads (): To parse JSON from String. json.load () to Parse JSON from a file.
Its human-readable format is easy to understand which makes writing programs, fetching data, and later on, modifying them easier. So that was all the way to validate JSON data return. To check valid JSON, there are no more offline tools required as you can do it on the go from any of your smart handheld devices.
Using JSON schema, we can make our choice scheme, so whenever we can validate the JSON document on this scheme, if passed, we can say that the form is valid JSON. You should follow the below-mentioned steps when dealing with JSON data files and validating their syntax and semantics. Define the Schema: Describe what you expect JSON.
JSON Validator JSON Validator is a free web-based tool that provides you the easiest and fastest way to validate your JSON code without any hurdles. This online tool enables you to find any error in JSON within a few seconds.
You might consider jsonschema
to validate your JSON. Here is a program that validates your example. To extend this to your "20 keys", add the key names to the "required"
list.
import jsonschema
import json
schema = {
"type": "object",
"properties": {
"customer": {
"type": "object",
"required": ["lastName", "firstName", "age"]}},
"required": ["service", "customer"]
}
json_document = '''{
"service" : "Some Service Name",
"customer" : {
"lastName" : "Kim",
"firstName" : "Bingbong",
"age" : "99"
}
}'''
try:
# Read in the JSON document
datum = json.loads(json_document)
# And validate the result
jsonschema.validate(datum, schema)
except jsonschema.exceptions.ValidationError as e:
print("well-formed but invalid JSON:", e)
except json.decoder.JSONDecodeError as e:
print("poorly-formed text, not JSON:", e)
Resources:
First, don’t check for None using if x == None
, use if x is None
You might try to simplify it slightly by checking the keys using set(json_data.keys()) == set(key1, key2, ...)
That would need to be repeated for any nested dictionaries in your json structure. Using sets instead of lists has the benefit of sets being unordered so it doesn’t matter the order of the data in your json.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With