jsonfinder lets you find and extract JSON objects and arrays embedded within a string using a simple iterator. For example:
>>> s = 'jack [1,2,3] john' >>> for start, end, obj in jsonfinder(s): ... print start, ':', end ... if obj is None: ... print 'String:', repr(s[start:end]) ... else: ... print 'List of length', len(obj) 0 : 5 String: 'jack ' 5 : 12 List of length 3 12 : 17 String: ' john' >>> assert len(s) == end
Two other convenience methods are also provided: has_json and only_json:
>>> has_json('stuff {"key": "value"} things')
True
>>> has_json('stuff only')
False
>>> only_json('stuff {"key": "value"} things')[2]
{u'key': u'value'}
>>> only_json('stuff only')
Traceback (most recent call last):
...
ValueError: No JSON object found in argument.
>>> only_json('{}{}')
Traceback (most recent call last):
...
ValueError: More than one JSON object found in the argument.
All the methods allow a custom JSONDecoder to be passed in for flexibility.
The library also includes a command-line tool to format JSON and filter out parts of the input based on whether JSON is present. It’s like a more flexible version of python’s built in json.tool, a grep -v for JSON, and more. For example:
$ cat cli_example.txt
This line contains no JSON and will be deleted by the --delete other-lines option.
On the other hand {"json": ["is", "formatted"] } and text surrounding it is preserved (but can also be removed if desired).
$ jsonfinder -i cli_example.txt --delete other-lines
On the other hand {
"json": [
"is",
"formatted"
]
} and text surrounding it is preserved (but can also be removed if desired).
$ jsonfinder -i cli_example.txt --delete other-lines | jsonfinder --delete context --format tiny
{"json":["is","formatted"]}
See the --help option in the command-line for more details.
Installation is as simple as:
pip install jsonfinder
Metadata
Release files for jsonfinder 0.4.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| jsonfinder-0.4.2.tar.gz | 9.5 kB | Without provenance | Details for jsonfinder-0.4.2.tar.gz |
Release files / jsonfinder-0.4.2.tar.gz
| Download URL | jsonfinder-0.4.2.tar.gz |
|---|---|
| Size | 9.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
9845ca2d1e54aa007a2a72aaa07dcc90f516587cfe40cb6df214c9cf19bc1f6f
|
|
BLAKE2b-256 checksum How to use checksums |
9b1b4590d10d1770f319dd79330cf9cb0feb4a7c4b5ff27c4cfc2d543d291898
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
Security for jsonfinder 0.4.2
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.