Skip to main content

Traversing and manipulating hierarchical info sets (JSON) using pythonic JSON Path-like expressions

Project description

aweson

Traversing and manipulating hierarchical data (JSON) using pythonic JSON Path-like expressions

Import the necessary stuff

from aweson import JP, find_all

Iterating over hierarchical data

content = {"employees": [{"name": "Doe, John", "age": 32}, {"name": "Doe, Jane", "age": -23}, {"name": "Deer, Jude", "age": 42}, ]} list(find_all(content, JP.employees[:].name)) ['Doe, John', 'Doe, Jane', 'Deer, Jude']

Note that the JSON Path-like expression is not a string, it's a Python expression, i.e. your IDE will be of actual help.

Furthermore, to address all items in a list, Pythonic slice expression [:] is used. Naturally, other indexing and slice expressions also work in the conventional Pythonic way:

list(find_all(content, JP.employees[-1].name)) ['Deer, Jude']

list(find_all(content, JP.employees[:2].name)) ['Doe, John', 'Doe, Jane']

You may be interested in the actual path of an item being returned, just like you get an index alongside items when using enumerate(). For instance, you may want to verify ages being non-negative, and report accurately the path of failure items:

path, item = next(t for t in find_all(content, JP.employees[:].age, enumerate=True) if t[1] < 0) item -23

Note, if argument enumerate=True is passed, find_all() returns tuples instead of single items: the first items being JSON Path-like expression (a pointer to data, just like an index of enumerate()) and the second items the value itself.

The offending path, then, in human-readable format:

str(path) '.employees[1].age'

The enclosing record, using .parent attribute of the path obtained for the offending age:

next(find_all(content, path.parent)) {'name': 'Doe, Jane', 'age': -23}

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

aweson-1.0.0.tar.gz (8.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

aweson-1.0.0-py3-none-any.whl (5.3 kB view details)

Uploaded Python 3

File details

Details for the file aweson-1.0.0.tar.gz.

File metadata

  • Download URL: aweson-1.0.0.tar.gz
  • Upload date:
  • Size: 8.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.10.18

File hashes

Hashes for aweson-1.0.0.tar.gz
Algorithm Hash digest
SHA256 fab5d2ec997b9e79d2f8a11d983b1c6dbc2d7cf3843684511ed7abc5c77ee0bf
MD5 42f4c5c8246a156b9ae57d00c77c205a
BLAKE2b-256 3eae6e279bd29b575f69517be751c4fa9b471a7cd826dcf782f55d86cbdce7b8

See more details on using hashes here.

File details

Details for the file aweson-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: aweson-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 5.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.10.18

File hashes

Hashes for aweson-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 512d8b813116adc1fd564cf6319558d687fcec3b4df6079a8909f9300cc2eb7d
MD5 b230b1f5a925641a92e08454f1e25aa6
BLAKE2b-256 536b0049ca43f5bd8384ec9444fc3a36b9ac8729b70918b3bd2e2a24a77608a9

See more details on using hashes here.

Supported by

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