json-verifier
json-verifier is a package which allows a test to spot-check various values of a JSON object.
Installation
pip install json-verifier
Why do We Need It?
Let's pretend that we are testing an API, which returns a JSON object such as:
{
"metadata": {
"name": "sandbox",
"tags": ["scratch", "pre-production", "experimental"]
}
}
Let say we need to check a few things:
- The name must be "staging"
- The description must be "Team sandbox"
- The first tag must be "testing"
- The fourth tag must be "non-production"
A typical test then would look like this:
def test_api():
actual = call_api()
assert actual["metadata"]["name"] == "staging"
assert actual["metadata"]["description"] == "Team sandbox"
assert actual["metadata"]["tags"][0] == "testing"
assert actual["metadata"]["tags"][3] == "non-production"
While this test works, it contains a couple of issues:
- We want to check for all issues, but the above will stop at the first assertion failure and will not test the rest
- Notice that the actual value does not contain a "description" value. That means the expression
actual["metadata"]["description"]will generate aKeyErrorexception - Likewise, the expression
actual["metadata"]["tags"][3]will generate anIndexErrorexception - It would be nice to be able to shorthand the keys, especially when dealing with a deeply nested JSON object
json-verifier is looking to address these issues. Consider the following test:
from json_verifier import JsonVerifier
def test_api(verifier):
actual = call_api()
with JsonVerifier(actual) as verifier:
verifier.verify_value("metadata.name", "staging")
verifier.verify_value("metadata.description", "Team sandbox")
verifier.verify_value("metadata.tags.0", "testing")
verifier.verify_value("metadata.tags.3", "non-production")
Here is a sample pytest output:
AssertionError: Verify JSON failed
Object:
{
"metadata": {
"name": "sandbox",
"tags": [
"scratch",
"pre-production",
"experimental"
]
}
}
Errors:
- path='metadata.name', expected='staging', actual='sandbox'
/home/user/workspace/test_api.py(11) in test_api()
verifier.verify_value("metadata.name", "staging")
- path='metadata.description', expected='Team sandbox', key error: 'description'
/home/user/workspace/test_api.py(12) in test_api()
verifier.verify_value("metadata.description", "Team sandbox")
- path='metadata.tags.0', expected='testing', actual='scratch'
/home/user/workspace/test_api.py(13) in test_api()
verifier.verify_value("metadata.tags.0", "testing")
- path='metadata.tags.3', expected='non-production', index error: 3
/home/user/workspace/test_api.py(14) in test_api()
verifier.verify_value("metadata.tags.3", "non-production")
This output shows some important information:
1. A formatted dump of the object under test
2. A list of errors. Each complete with filename, line number, the function, the offending line, and why test failed
Usage
For additional usage tips, see usage.md
License
json-verifier is distributed under the terms of the MIT license.
Metadata
Release files for json-verifier 0.6.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| json_verifier-0.6.1.tar.gz | 21.5 kB | Without provenance | Details for json_verifier-0.6.1.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| json_verifier-0.6.1-py3-none-any.whl | Python 3 | none | any | Details for json_verifier-0.6.1-py3-none-any.whl |
Total release size: 26.2 kB
Release files / json_verifier-0.6.1.tar.gz
| Download URL | json_verifier-0.6.1.tar.gz |
|---|---|
| Size | 21.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
3cac4bc093cbeffdde704118c808e512326b5d15f94c51b646f5e7bb266db5fb
|
|
BLAKE2b-256 checksum How to use checksums |
5bcc80e845ecb775f1f24307853807598169bf0ccf51ec95187226aae13eac29
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.9.22
|
Release files / json_verifier-0.6.1-py3-none-any.whl
| Download URL | json_verifier-0.6.1-py3-none-any.whl |
|---|---|
| Size | 4.7 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
fbf4c4fa9e4beff945c5e46a87555c6ae9bc096d74098274fa3352c513e4de15
|
|
BLAKE2b-256 checksum How to use checksums |
4505166368a2d091afb5b5ee8d189a35b796c26f5a4b97a02636815885ae13c4
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.9.22
|
Security for json-verifier 0.6.1
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.