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.5.1
5d30a84 Fix,Dev,CI:Release stage error
v0.5.0
2971509 New:Add –ensure-ascii argument. (closes #9)
1c6f602 New:Be able to use stand-alone parser.
c78505e Chg:Only release built distribution, wheel. (See #11)
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 Distributions
No source distribution files available for this release.See tutorial on generating distribution archives.
Built Distribution
Close
Hashes for jsonpath_extractor-0.5.1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | cbf1a08b11923bef500541399fc0c8f8032d7aae2517ae95d086748c50067dbf |
|
MD5 | e8b12d9d729f586d3e614ef194c7af43 |
|
BLAKE2b-256 | f8698a21e1885a8d23243c4761fcf1426224f3831639bb3e7bd8e4ff9e6d0d8a |