Giter Club home page Giter Club logo

jsonparse's Introduction

jsonparse: ctrl-f for json

PyPI - Python Version GitHub tag (latest SemVer) jsonparse codecov


jsonparse is a simple JSON parsing library. Extract what's needed from key:value pairs.

What's New

  • New public (or hostable) web API 🎉

Python Library

Install

pip install jsonparse

Quickstart

Here is a quick example of what jsonparse is able to do.

from jsonparse import Parser

p = Parser()

data = [{
    "key0":
    {
        "key1": "result",
        "key2":
        {
            "key1": "result1",
            "key3": {"key1": "result2"}
        }
    }
}]

p.find_key(data, 'key1')
['result2', 'result1', 'result']

p.find_key_chain(data, ['key0', 'key2', 'key3', 'key1'])
['result2']

API

The API examples using the following test data.

data = [
    {"key": 1},
    {"key": 2},
    {"my": 
        {"key": 
            {
                "chain": "A",
                "rope": 5,
                "string": 1.2,
                "cable": False
            }
        }
    },
    {"your":
    	{"key":
            {
                "chain": "B",
                "rope": 7,
                "string": 0.7,
                "cable": True
            }
    	}
    }
]

Parser

Parser(stack_trace: bool = False, queue_trace: bool = False)

    Optionally instantiate the Parser class with tracing to print out the underlying data structures.

p = Parser(stack_trace=True, queue_trace=True)

find_key

find_key(data: dict | list, key: str) -> list

    Will return all values of the matched key.

p.find_key(data, 'chain')
['A', 'B']

p.find_key(data, 'key')
[1, 2, {'chain': 'A', 'rope': 5, 'string': 1.2, 'cable': False}, {'chain': 'B', 'rope': 7, 'string': 0.7, 'cable': True}]

find_keys

find_keys(data: dict | list, keys: list, group: bool = True) -> list

    The default return value is a two dimensional list. [ [], [], ...].

    To return all values as a one dimensional list, set group=False.

    The ordering of the keys does not matter.

p.find_keys(data, ['rope', 'cable'])
[[5, False], [7, True]]

p.find_keys(data, ['rope', 'cable'], group=False)
[5, False, 7, True]

find_key_chain

find_key_chain(data: dict | list, keys: list) -> list

    The key chain is an ordered list of keys. The chain needs to start at the root level of the nested data.

    Wildcard * can be used as key(s) to match any.

p.find_key_chain(data, ['my', 'key', 'chain'])
['A']

p.find_key_chain(data, ['key'])
[1, 2]

p.find_key_chain(data, ['*', 'key', 'chain'])
['A', 'B']

p.find_key_chain(data, ['*', 'key', '*'])
['A', 5, 1.2, False, 'B', 7, 0.7, True]

find_key_value

find_key_value(data: dict | list, key: str, value: str | int | float | bool | None) -> list

    The returned list contains the dictionaries that contain the specified key:value pair.

p.find_key_value(data, 'cable', False)
[{'chain': 'A', 'rope': 5, 'string': 1.2, 'cable': False}]

p.find_key_value(data, 'chain', 'B')
[{'chain': 'B', 'rope': 7, 'string': 0.7, 'cable': True}]

Web API

Documentation

Visit the swagger API documentation

All endpoints are HTTP POST requests where you include the searchable JSON data in the request body.

Brief Endpoint Overiew

POST /v1/key/{key}
POST /v1/keys?key=1&key=2&key=3&key=4...
POST /v1/keychain?key=1&key=2&key=3&key=4...
POST /v1/keyvalue?key=a&value=1

Quickstart

Let's practice using the public, free-to-use-no-authentication, web API hosted in GCP Cloud Run.

We are POST'ing the JSON data with curl, requesting to search for the key, 'key1'. The found key values are returned as JSON.

curl -X POST "https://jsonparse.dev/v1/key/key1" \
-H 'Content-Type: application/json' \
-d '[{"key0":{"key1":"result","key2":{"key1":"result1","key3":{"key1":"result2"}}}}]'

["result2","result1","result"]

OR (using python and requests library)

import requests

data = [{
    "key0":
    {
        "key1": "result",
        "key2":
        {
            "key1": "result1",
            "key3": {"key1": "result2"}
        }
    }
}]

requests.post('https://jsonparse.dev/v1/key/key1', json=data).json()

['result2', 'result1', 'result']

Self-Hosted

pip install "jsonparse[webapi]"

gunicorn -b 0.0.0.0:8000 jsonparse.webapi:app

Alternatively, run the docker container

docker run -d ctomkow/jsonparse

jsonparse's People

Contributors

ctomkow avatar

Watchers

 avatar

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.