Coder Social home page Coder Social logo

linw1995 / jsonpath Goto Github PK

View Code? Open in Web Editor NEW
38.0 1.0 3.0 739 KB

A query expression for extracting data from JSON.

Home Page: https://jsonpath.rtfd.io/en/latest/

License: MIT License

Makefile 2.29% Python 97.71%
jsonpath data-extractor data-extraction cli

jsonpath's Introduction

JSONPATH

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

Build

  • Remove support for Python 3.7

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

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.