Skip to main content

pson

Python library to make querying JSON-like structures easy!

With the explosion of APIs returning JSON the use of JSON like structures (i.e what you get if you take JSON and run it through JSON.loads()) querying these types of structures is a common need.

Most people either manually traverse the structures or write their own querying logic, this library aims to solve that need by providing a general purpose querying utils. It uses the dot notation to traverse structures in a similar fashion to document databases such as MongoDB.

Examples

To demonstrate by example with the following JSON:

json = {
 'header' : {'title' : 'Hello World',
             'year' : 2014},
 'body' : { 'translations' : [
                                {'language' : 'french', 'translation' : 'bonjour'},
                                {'language' : 'german', 'translation' : 'guten tag'}
                             ]
          }
}

Traditional (pre-pson) approach:

>>> json['header']['title']
'Hello World'

New pson approach:

>>> from pson import pathquery as pq

>>> pq(json, 'header.title')
'Hello World'

Some more pson examples:

>>> from pson import pathquery as pq

>>> pq(json, 'header.title')
'Hello World'

# as you'd expect it can return sub-structures
>>> pq(json, 'body')
{'translations': [{'translation': 'bonjour', 'language': 'french'}, {'translation': 'guten tag', 'language': 'german'}]}

# and arrays
>>> pq(json, 'body.translations')
[{'translation': 'bonjour', 'language': 'french'}, {'translation': 'guten tag', 'language': 'german'}]

# you can query into arrays using numerical indicies
>>> pq(json, 'body.translations.0.language')
'french'

# you can also not specify an index and get back an array where the rest of the query
# is applied to every element in the array
>>> pq(json, 'body.translations.language')
['french', 'german']

How does it handle missing values ?

>>> from pson import pathquery as pq

# by default it returns None rather than throwing an exception
>>> pq(json, 'header.author')
None

# you can over-ride what it returns though
>>> pq(json, 'header.author', missing="Unknown")
'Unknown'

# which is useful for situations like where you're building strings and don't want to deal
# with lots of KeyError exceptions
>>> pq(json, 'header.title') + ' by ' + pq(json, 'header.author', missing="Unknown")
'Hello World by Unknown'

Installation

TBD

Authors

Built by Imran Ghory (@imranghory). Released under the MIT Licence. Contributions welcome via Github.

Metadata

Release files for pson 0.5.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 pson 0.5.1
File Size Uploaded Provenance Actions
pson-0.5.1.tar.gz 4.8 kB Without provenance Details for pson-0.5.1.tar.gz

Release files / pson-0.5.1.tar.gz

Download URL pson-0.5.1.tar.gz
Size 4.8 kB
Tags Source
SHA-256 checksum
How to use checksums
5b15c2f49d65c390dbab1123d3ec495f47ae0c091e7595b56cb95729c44ba757
BLAKE2b-256 checksum
How to use checksums
0e964aa51228b789fd69654cd51a48a9bdf3bf38d1271899dd8ff347afb7b2f7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Security for pson 0.5.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.5.1 This release

1 release file No provenance

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