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

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

  • a4e3dee Chg:Refactoring
  • f46e87e Fix:Exports requirements.txt error
  • c085900 New:Supports Python3.9
  • 3f8b882 Fix:mypy error when using Python39
  • 3b1a40a Fix:Missing Python3.9
  • 53905c2 Chg:Update Brace class doc.
  • ad76217 Chg:Update Brace class doc.
  • c4d9538 Fix:Build document first while running ‘make live_docs’
  • b12491e Fix,Dev:Must deactivate before using nox
  • 82ada7a Fix:build.py file contamination (fixes #26)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for jsonpath-extractor, version 0.7.3
Filename, size File type Python version Upload date Hashes
Filename, size jsonpath_extractor-0.7.3-py3-none-any.whl (38.3 kB) File type Wheel Python version py3 Upload date Hashes View
Filename, size jsonpath-extractor-0.7.3.tar.gz (38.0 kB) File type Source Python version None Upload date Hashes View

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring DigiCert DigiCert EV certificate Facebook / Instagram Facebook / Instagram PSF Sponsor Fastly Fastly CDN Google Google Object Storage and Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Salesforce Salesforce PSF Sponsor Sentry Sentry Error logging StatusPage StatusPage Status page