Your friendly neighborhood JSON sorter helper
Project description
sansJson
Your friendly neighborhood JSON sorter helper!
This package is meant to rival the native sort_keys
option of the json
package. sort_keys
only re-orders the keys according to the alphanumeric
priority. This method breaks down when internal components (such as lists
)
need to be re-ordered as well. To ensure consistency across the entire JSON
object, as in the event of a hash
, a recursive reordering of sortable
components may be necessary. The following situations are covered:
Note:
-
Homogenous: All elements in a specific component are the same data type. (e.g.
{'a': [1,2,3], 'b', [2,3,4], 'c': [3,4,5]}
) -
Nonhomogenous: Each element in a specific component may be any of the supported JSON types. (e.g.
{'a': [1, 'z', False], 0: [3,4,5]}
) -
The default priority for nonhomogenous types is:
null
boolean
number (int > float)
string
array
object
-
Multi-level
dict
JSON (homogenous + nonhomogenous) -
array
JSON (homogenous + nonhomogenous)- All types except python
object
- All types except python
The following cases are not supported:
- python
objects
(i.e. classes)
Installation
pip install sansjson
How to use
>>> import sansjson
>>> to_sort = {'a': 0, 'g': [3, 1, 3, 2], 'c': 1, 'f': 2}
>>> sansjson.sort_pyobject(to_sort)
{'a': 0, 'c': 1, 'f': 2, 'g': [1, 2, 3, 3]}
>>> a = '{"z": 1, "a": {"12": true, "ef": ["a", 1, false], "ap": 0}}'
>>> sansjson.sort_json(a)
'{"a": {"12": true, "ap": 0, "ef": [false, 1, "a"]}, "z": 1}'
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.