Sorts a JSON or YAML document to match a JSON Schema's order of properties
Project description
Provides JSON tools:
-
jschon-sort
sorts a JSON or YAML document according to its JSON Schema: object properties are ordered to match the order in which JSON Schema properties (that match them) are declared. -
jschon-remove-additional-props
removes properties not defined in the JSON Schema.
The "jschon" name relates to it being based on the jschon library for JSON Schema handling.
Motivation
Per the JSON RFC, an object is an unordered collection. In practice, within serialized JSON or YAML files,
a particular order of properties can benefit readability: for example,
{"start": 10, "end": 20}
read more naturally than naive lexicographic order of {"end": 20, "start": 10}
(that would result from json.dumps(..., sort_keys=True)
).
While there are several
attempts
to introduce property ordering into JSON Schema, here we're taking a different approach.
By leveraging the fact that the JSON Schema itself is written with human maintainers in mind,
we can extrapolate the intuitive order from the JSON Schema definitions' ordering and apply it on the document itself.
Usage
Shell:
jschon-sort --schema ../schema.json file.yaml
API:
import jschon
import jschon_tools
jschon.create_catalog('2020-12')
...
sorted_doc_data = jschon_tools.process_json_doc(
schema_data=schema_data,
doc_data=doc_data,
sort=True,
)
Example
Given schema:
{
"type": "object",
"properties": {
"range": {
"type": "object",
"properties": {
"start": {"type": "number"},
"end": {"type": "number"}
}
}
}
}
the following document:
{"range": {"end": 20, "start": 10}}
would be reordered as:
{"range": {"start": 20, "end": 10}}
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
File details
Details for the file jschon-sort-0.1.0.tar.gz
.
File metadata
- Download URL: jschon-sort-0.1.0.tar.gz
- Upload date:
- Size: 5.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/32.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.62.3 importlib-metadata/4.11.1 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.9.10
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 58e36302de68a17dd23c6e13ce42ea6da9fbd6cd55a2ec9a0efcea7efc1a7539 |
|
MD5 | f352ced34b40d9f0648169c283b0f710 |
|
BLAKE2b-256 | fd6f23565325c76df673d5457520ca04d8974870dbdd59fff1bd6f785c689b4a |