Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check the completeness of JSON data

Tags:

python

json

I get data in JSON from an API and it may be that the received data is not complete (= some fields are missing). I am not sure either that the structure of the data follows JSON standards.

The solution for the second problem is simple: I will try: to decode the JSON and act accordingly on ValueError and TypeError exceptions.

For the first problem, my solution would also be to

d = {'a': 1}
try:
    d['a']
    d['b']
    d['x']['shouldbethere']
except KeyError:
(...)

that is to list all the keys I need to have in the dict created from a successful JSON conversion.

This made me think that there may be a method to declare the expected keys (and possibly values types) and match the retrieved JSON against it, an unsuccessful match raising a specific exception?

like image 604
WoJ Avatar asked Dec 03 '25 15:12

WoJ


1 Answers

Standard way to validate JSON structure is to use JSON Schema. Basic characteristics (quoted from official webpage) are:

JSON Schema:

  • describes your existing data format
  • clear, human- and machine-readable documentation
  • complete structural validation, useful for
    • automated testing
    • validating client-submitted data

There is no built-in package to validate JSON object against schema, although you may use jsonschema from pypi.

Sample usage (paraphrased from official docs) may be:

import jsonschema

schema = {
    "type": "object",
    "properties": {
        "price": {"type": "number"},
        "name": {"type": "string"},
    },
}

jsonschema.validate({"name": "Eggs", "price": 34.99}, schema)
# No exception from line above - document is valid
jsonschema.validate({"name": "Eggs", "price": "Invalid"}, schema)
# ValidationError: 'Invalid' is not of type 'number'
like image 145
Łukasz Rogalski Avatar answered Dec 06 '25 05:12

Łukasz Rogalski



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!