Skip to main content

Sorts a JSON or YAML document to match a JSON Schema's order of properties

Project description

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.

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_sort

jschon.create_catalog('2020-12')
...
sorted_doc_data = jschon_sort.sort_doc_by_schema(
    schema_data=schema_data,
    doc_data=doc_data,
)

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

jschon-sort-0.0.2.tar.gz (4.9 kB view details)

Uploaded Source

File details

Details for the file jschon-sort-0.0.2.tar.gz.

File metadata

  • Download URL: jschon-sort-0.0.2.tar.gz
  • Upload date:
  • Size: 4.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.6.0 importlib_metadata/4.8.2 pkginfo/1.8.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.9

File hashes

Hashes for jschon-sort-0.0.2.tar.gz
Algorithm Hash digest
SHA256 04db598cdd7f18a04b045d16a8741c9c47e33eb3857c2ea2bd6340c1eda051cc
MD5 41c1cbeeb933d231d9f89663507ba9a4
BLAKE2b-256 2cf578f1e98e4f03e42c60aa8a1afbbcef5b229a088277cf7e71654cfda7eb75

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page