Skip to main content

A selector expression for extracting data from JSON.

Project description

license Pypi Status Python version Package version PyPI - Downloads GitHub last commit Code style: black Build Status codecov PDM managed

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.9.1

Build

  • Remove support for Python 3.8

Contributing

Environment Setup

Clone the source codes from Github.

git clone https://github.com/linw1995/jsonpath.git
cd jsonpath

Setup the development environment. Please make sure you install the pdm, pre-commit and nox CLIs in your environment.

make init
make PYTHON=3.8 init  # for specific python version

Linting

Use pre-commit for installing linters to ensure a good code style.

make pre-commit

Run linters. Some linters run via CLI nox, so make sure you install it.

make check-all

Testing

Run quick tests.

make

Run quick tests with verbose.

make vtest

Run tests with coverage. Testing in multiple Python environments is powered by CLI nox.

make cov

Documentation

Run serving documents with live-reloading.

make serve-docs

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

jsonpath-extractor-0.9.2.tar.gz (49.4 kB view details)

Uploaded Source

Built Distribution

jsonpath_extractor-0.9.2-py3-none-any.whl (46.1 kB view details)

Uploaded Python 3

File details

Details for the file jsonpath-extractor-0.9.2.tar.gz.

File metadata

  • Download URL: jsonpath-extractor-0.9.2.tar.gz
  • Upload date:
  • Size: 49.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.9.20

File hashes

Hashes for jsonpath-extractor-0.9.2.tar.gz
Algorithm Hash digest
SHA256 374fa9a03755d275808f208fedb13966c5e2b654de1f2b59844f00d2d8a023a1
MD5 3363cbf6ee615052cca8c26a2c21aba6
BLAKE2b-256 2289cbb9755ffb6a622e3cd76f9b952d62680b05b8f3618e7e141b074702a688

See more details on using hashes here.

File details

Details for the file jsonpath_extractor-0.9.2-py3-none-any.whl.

File metadata

File hashes

Hashes for jsonpath_extractor-0.9.2-py3-none-any.whl
Algorithm Hash digest
SHA256 a79a7a44ebce35538e68d4b7d5ed155a914f942755fcce0a0812b488b794700d
MD5 bd7850e1414d943bc6bbf987e1b89816
BLAKE2b-256 fcd4a2516842f2e8b49dc3ef7e7d29214709180966c45c1863898cc6720e6b17

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page