A selector expression for extracting data from JSON.
Project description
A selector expression for extracting data from JSON.
Quickstarts
Installation
Install the stable version from PYPI.
pip install jsonpath-extractor
Or install the latest version from Github.
pip install git+https://github.com/linw1995/jsonpath.git@master
Usage
{
"goods": [
{"price": 100, "category": "Comic book"},
{"price": 200, "category": "magazine"},
{"price": 200, "no category": ""}
],
"targetCategory": "book"
}
How to parse and extract all the comic book data from the above JSON file.
import json
from jsonpath import parse
with open("example.json", "r") as f:
data = json.load(f)
assert parse("$.goods[contains(@.category, $.targetCategory)]").find(
data
) == [{"price": 100, "category": "Comic book"}]
Or use the jsonpath.core module to extract it.
from jsonpath.core import Root, Contains, Self
assert Root().Name("goods").Predicate(
Contains(Self().Name("category"), Root().Name("targetCategory"))
).find(data) == [{"price": 100, "category": "Comic book"}]
Usage via CLI
The faster way to extract by using CLI.
jp -f example.json "$.goods[contains(@.category, $.targetCategory)]"
Or pass content by pipeline.
cat example.json | jp "$.goods[contains(@.category, $.targetCategory)]"
The output of the above commands.
[
{
"price": 100,
"category": "Comic book"
}
]
Changelog
v0.7.2
5741909 New:Add a new context variable “parent”
dde3703 New:Add a helper function ‘temporary_set’
3336e86 New:Be able to get parent node while searching
901354f Fix:Slice nested exprs find erorr
c0e82d6 Fix:Slice.find apply on list only
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for jsonpath_extractor-0.7.2-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 64a4aedd2a6b927a1e52f295ce29104f38ef1a28871b0941ad42967e85e0689c |
|
MD5 | 4d3ec204885a7f7e34abae98bb0f6f32 |
|
BLAKE2b-256 | 9ce58acc8fea5d511880e487406010cbdfff7ab62c5da7b970fc9d878e750d51 |