Skip to main content

nested-json

PyPI PyPI - Downloads GitHub release (latest by date)

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 one
  • parse(str) - to parse a JSON string to a nested JSON
  • process(obj) - to process/stringify any nested JSON
  • dumps(obj), dump(obj, fp) - same as from the json module, but supports nested JSON
  • loads(str), load(fp) - same as from the json 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.

Release files for nested-json 1.0.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for nested-json 1.0.1
File Size Uploaded
nested_json-1.0.1.tar.gz 4.5 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for nested-json 1.0.1
File Interpreter ABI Platform
nested_json-1.0.1-py3-none-any.whl Python 3 none any Details

Total release size: 9.0 kB

Release files / nested_json-1.0.1.tar.gz

Download URL nested_json-1.0.1.tar.gz
Size 4.5 kB
Tags Source
SHA-256 checksum
How to use checksums
a921b4a80c666cfe1600dd5cf31786fcceea20231a2fc6219814f96758cd0cc4
BLAKE2b-256 checksum
How to use checksums
227698982f1ab4a3a79282e5f4bae77bf292b1731a745da3e58f1e61818aa112
Upload date
Uploaded using Trusted Publishing?
What is 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

Release files / nested_json-1.0.1-py3-none-any.whl

Download URL nested_json-1.0.1-py3-none-any.whl
Size 4.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
344e3451403f2423edee7ee78c68c847419917decbd5600030391544255e0bdd
BLAKE2b-256 checksum
How to use checksums
6365102dfa761cfcf30e78789e6819f7bac411aa8c88beef5e8bb40e5ff54e33
Upload date
Uploaded using Trusted Publishing?
What is 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

Release history Release notifications | RSS feed

This release

1.0.1 This release

2 release files

1.0.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page