A library to manage JSONs with encoded JSON more effectively (e.g. AWS Event Bridge Events, AWS API Gateway Events)
Project description
nested-json
A library to manage JSONs with encoded JSON more effectively (e.g. AWS Event Bridge Events, AWS API Gateway Events)
Install
pip install nested_json
Usage
import nested_json as njson
The main API of the library consist of:
Nested(obj)
- to convert any list or dictionary to a nested oneparse(str)
- to parse a JSON string to a nested JSONprocess(obj)
- to process/stringify any nested JSONdumps(obj)
,dump(obj, fp)
- same as from thejson
module, but supports nested JSONloads(str)
,load(fp)
- same as from thejson
module, but supports nested JSON
Marking dict
/ list
as nested
The Nested
function can be used to mark a dictionary or
list as nested data, which will add a particular property to it,
marking it nested. This property was later removed during processing.
data = {
"id": "12345",
"event": {
"payload": {
"rawpayload": njson.Nested({
"key": "value",
"other": 2,
"foo": True
}),
"tags": njson.Nested([
"hello",
"world
])
}
}
}
# {
# "id": "12345",
# "event": {
# "payload": {
# "rawpayload": {
# "__nested__": True,
# "key": "value",
# "other": 2,
# "foo": True
# },
# "tags": [
# "__nested__",
# "hello",
# "world
# ]
# }
# }
# }
Processing nested JSON
Using the process
function, the nested keys can be removed
and nested data converted to JSON string.
processed_data = njson.process(data)
# {
# "id": "12345",
# "event": {
# "payload": {
# "rawpayload": "{\"key\": \"value\", \"other\": 2, \"foo\": true}",
# "tags": "[\"hello\", \"world\"]"
# }
# }
# }
Converting to JSON string
The result of process
can be already passed to json.dumps
,
but the njson.dumps
can also be used with nested JSON data.
json_string = njson.dumps(data)
# '{"id": "12345", "event": {"payload": {"rawpayload": "{\\"key\\": \\"value\\", \\"other\\": 2, \\"foo\\": true}", "tags": "[\\"hello\\", \\"world\\"]"}}}'
Parsing JSON string
Nested JSON string can be parsed to nested JSON data with the loads
function or the parse
function
assert njson.loads(json_string) == data
assert njson.parse(processed_data) == data
assert njson.parse(json.loads(json_string)) == data
Compatibility
Note that both loads
and dumps
use the json.loads
and json.dumps
functions; thus they can be used with "normal" JSON as well.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file nested_json-1.0.1.tar.gz
.
File metadata
- Download URL: nested_json-1.0.1.tar.gz
- Upload date:
- Size: 4.5 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.10.1 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.9.10
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | a921b4a80c666cfe1600dd5cf31786fcceea20231a2fc6219814f96758cd0cc4 |
|
MD5 | 2bbda80ff1e26aa3e924df6e32583a6c |
|
BLAKE2b-256 | 227698982f1ab4a3a79282e5f4bae77bf292b1731a745da3e58f1e61818aa112 |
File details
Details for the file nested_json-1.0.1-py3-none-any.whl
.
File metadata
- Download URL: nested_json-1.0.1-py3-none-any.whl
- Upload date:
- Size: 4.5 kB
- Tags: Python 3
- 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.10.1 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.9.10
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 344e3451403f2423edee7ee78c68c847419917decbd5600030391544255e0bdd |
|
MD5 | ede9ab7fb32df4a1263d19468dc0db8d |
|
BLAKE2b-256 | 6365102dfa761cfcf30e78789e6819f7bac411aa8c88beef5e8bb40e5ff54e33 |