Skip to main content

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

Project description

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

Importing

>>> from aweson import JP, find_all

Iterating over hierarchical data

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

Note that the JSON Path-like expression JP.employees[:].name 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']

Paths to items iterated

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(tup for tup in find_all(content, JP.employees[:].age, enumerate=True) if tup[1] < 0)
>>> item
-23

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, 'account': 'janedoe'}

Note, with argument enumerate=True passed, find_all() yields tuples instead of single items.

Selecting sub-items

You can select sub-items of iterated items, comes handy into turning one structure into another, like a list of records into a dict:

>>> {tup[0]: tup[1] for tup in find_all(content, JP.employees[:](JP.account, JP.name))}
{'johndoe': 'Doe, John', 'janedoe': 'Doe, Jane', 'judedeer': 'Deer, Jude'}

Note, how JP.employees[:](JP.account, JP.name) selects a tuple of respective fields.

Variable field name selection

The forms JP["field_name"] and JP.field_name are equivalent:

>>> from functools import reduce
>>> def my_sum(content, field_name, initial):
...     return reduce(
...         lambda x, y: x + y,
...         find_all(content, JP.employees[:][field_name]),
...         initial
...     )
>>> my_sum(content, "age", 0)
51
>>> my_sum(content, "account", "")
'johndoejanedoejudedeer'

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.1.tar.gz (9.2 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.1-py3-none-any.whl (5.7 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: aweson-1.0.1.tar.gz
  • Upload date:
  • Size: 9.2 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.1.tar.gz
Algorithm Hash digest
SHA256 a67465ff20c1182c14417e748f287f387605cd268b456cb080e90437e5d74b86
MD5 357de3e2205897eed5e030ec81dc2aca
BLAKE2b-256 ee7a89f461c03067a1a25049b6055be5089cb5749b51d3b4fbe624ab39aa9343

See more details on using hashes here.

File details

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

File metadata

  • Download URL: aweson-1.0.1-py3-none-any.whl
  • Upload date:
  • Size: 5.7 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.1-py3-none-any.whl
Algorithm Hash digest
SHA256 4acbdda916809bba2641cb2d9b20fc12a1451b84b9e94f3dca487bbf53ee67e5
MD5 0442de80953c8a7438d663470ae3db04
BLAKE2b-256 d258b776c28dd91375a0b6f885bdd7fca2777b7d62db0f8f155050e7cfa66ece

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