How to check the completeness of JSON data

WoJ

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?

Łukasz Rogalski

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'

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Samba: check file for completeness

From Dev

Programmatically check textboxes for completeness

From Dev

Calculating and reporting Data Completeness

From Dev

Referential completeness integrity check in SQL

From Dev

Referential completeness integrity check in SQL

From Dev

How to check for json data exist?

From Dev

How to check for json data exist?

From Dev

How do you calculate data completeness for multiple tables based on null values within columns?

From Dev

How do you calculate data completeness for multiple tables based on null values within columns?

From Dev

How to check if the value from the json Data is null

From Dev

How to check if json data return is empty with jquery

From Dev

How to check the JSON Store data on the device

From Dev

How to check if a value/property exist in JSON data

From Dev

How to check empty data Json parse JavaScript?

From Dev

How to check if the value from the json Data is null

From Dev

Ensuring Data completeness and validity on third party storage

From Dev

json check empty data

From Dev

How can I bind JSON data to check box in JQuery

From Dev

How to check for required parameters in case of JSON type form data

From Dev

how to check JsonObject has null value(windows.data.json)

From Dev

How do you check if JSON data is an object, or a String?

From Dev

How to check in NodeJS if json file already got specific data

From Dev

How to check a Windows.Data.Json.JsonObject for certain value?

From Dev

How to add profile completeness progress bar in php and wordpress?

From Dev

How did Haskell add Turing-completeness to System F?

From Dev

How to put a check on Jquery Data that either the data is in form of JSON or a simple HTML?

From Dev

One json param contains data in array or object. How to check data is array or object using GSON

From Dev

how to check the check boxes with retrieved data

From Java

How to check if a json key exists?

Related Related

  1. 1

    Samba: check file for completeness

  2. 2

    Programmatically check textboxes for completeness

  3. 3

    Calculating and reporting Data Completeness

  4. 4

    Referential completeness integrity check in SQL

  5. 5

    Referential completeness integrity check in SQL

  6. 6

    How to check for json data exist?

  7. 7

    How to check for json data exist?

  8. 8

    How do you calculate data completeness for multiple tables based on null values within columns?

  9. 9

    How do you calculate data completeness for multiple tables based on null values within columns?

  10. 10

    How to check if the value from the json Data is null

  11. 11

    How to check if json data return is empty with jquery

  12. 12

    How to check the JSON Store data on the device

  13. 13

    How to check if a value/property exist in JSON data

  14. 14

    How to check empty data Json parse JavaScript?

  15. 15

    How to check if the value from the json Data is null

  16. 16

    Ensuring Data completeness and validity on third party storage

  17. 17

    json check empty data

  18. 18

    How can I bind JSON data to check box in JQuery

  19. 19

    How to check for required parameters in case of JSON type form data

  20. 20

    how to check JsonObject has null value(windows.data.json)

  21. 21

    How do you check if JSON data is an object, or a String?

  22. 22

    How to check in NodeJS if json file already got specific data

  23. 23

    How to check a Windows.Data.Json.JsonObject for certain value?

  24. 24

    How to add profile completeness progress bar in php and wordpress?

  25. 25

    How did Haskell add Turing-completeness to System F?

  26. 26

    How to put a check on Jquery Data that either the data is in form of JSON or a simple HTML?

  27. 27

    One json param contains data in array or object. How to check data is array or object using GSON

  28. 28

    how to check the check boxes with retrieved data

  29. 29

    How to check if a json key exists?

HotTag

Archive