jsonparse


Namejsonparse JSON
Version 0.15.1 PyPI version JSON
download
home_pageNone
Summaryctrl-f for JSON. A simple JSON parsing library. Extract what's needed from key:value pairs.
upload_time2024-10-12 03:35:21
maintainerNone
docs_urlNone
authorNone
requires_python!=3.0,!=3.1,!=3.2,!=3.3,!=3.4,!=3.5,>=2.7
licenseNone
keywords json filter key value parse parsing python search searching
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # jsonparse: ctrl-f for json
![PyPI - Python Version](https://img.shields.io/pypi/pyversions/jsonparse)
![GitHub tag (latest SemVer)](https://img.shields.io/github/v/tag/ctomkow/jsonparse?label=version&sort=semver)
[![jsonparse](https://github.com/ctomkow/jsonparse/actions/workflows/jsonparse-buildtest.yml/badge.svg)](https://github.com/ctomkow/jsonparse/actions/workflows/jsonparse-buildtest.yml)
[![codecov](https://codecov.io/gh/ctomkow/jsonparse/branch/master/graph/badge.svg?token=affX7FZaFk)](https://codecov.io/gh/ctomkow/jsonparse)
![Static Badge](https://img.shields.io/badge/powered_by-mead-E79A3F?style=flat)


</br>

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

## What's New
 - Python 2.7 compat. :sweat_smile: :relieved:
 - A new function, [find_value](#find_value), has been added. This function will return all keys of the matched value. :grinning:
 - [CLI tool](#CLI-tool). Parse json text files or stdin via the command line :tada:

# Python Library

## Install
```bash
pip install jsonparse
```

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

```python
from jsonparse import find_key, find_keys, find_key_chain, find_key_value, find_value

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

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

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

:heavy_plus_sign: See additional documentation in the [API section](#API) below.


# CLI tool

## Install
```bash
pip install jsonparse
```

## Quickstart
Summary of cli commands. For complete information, `jp --help`

Note, `jsonparse` and `jp` are equivalent.

`jp key key1 --file text.json`

`jp keys key1 key2 key3 --file text.json`

`jp key-chain my '*' chain --file text.json`

`jp key-value key1 '"result"' --file text.json`

`echo '{"key1": {"key2": 5}}' | jp key key2`

`jp value null --file text.json`

`jp value 42 --file text.json`

`jp value '"strValue"' --file text.json`


# API

- [jsonparse functions](#Functions)
    - [find_key](#find_key)
    - [find_keys](#find_keys)
    - [find_key_chain](#find_key_chain)
    - [find_key_value](#find_key_value)
    - [find_value](#find_value)

The API examples using the following test data.

```python
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
            }
    	}
    }
]
```

---

### Functions

```python
from jsonparse import find_key, find_keys, find_key_chain, find_key_value, find_value
```

---

### find_key
<pre>
<b>find_key(</b><i>data</i>: dict | list | OrderedDict, <i>key</i>: str<b>)</b> -> list
</pre>

&nbsp;&nbsp;&nbsp;&nbsp;Will return all values of the matched key.

```python
find_key(data, 'chain')
['A', 'B']

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
<pre>
<b>find_keys(</b><i>data</i>: dict | list | OrderedDict, <i>keys</i>: list, <i>group</i>: bool = True<b>)</b> -> list
</pre>

&nbsp;&nbsp;&nbsp;&nbsp;The default return value is a two dimensional list. `[ [], [], ...]`.

&nbsp;&nbsp;&nbsp;&nbsp;To return all values as a one dimensional list, set `group=False`.

&nbsp;&nbsp;&nbsp;&nbsp;The ordering of the keys does not matter.

```python
find_keys(data, ['rope', 'cable'])
[[5, False], [7, True]]

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

---

### find_key_chain
<pre>
<b>find_key_chain(</b><i>data</i>: dict | list | OrderedDict, <i>keys</i>: list<b>)</b> -> list
</pre>

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

&nbsp;&nbsp;&nbsp;&nbsp;Wildcard `*` can be used as key(s) to match any.

```python
find_key_chain(data, ['my', 'key', 'chain'])
['A']

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

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

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

---

### find_key_value
<pre>
<b>find_key_value(</b><i>data</i>: dict | list | OrderedDict, <i>key</i>: str, <i>value</i>: str | int | float | bool | None) -> list
</pre>

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

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

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

---

### find_value
<pre>
<b>find_value(</b><i>data</i>: dict | list | OrderedDict, <i>value</i>: str | int | float | bool | None<b>)</b> -> list
</pre>

&nbsp;&nbsp;&nbsp;&nbsp;Will return all keys of the matched value.

```python
find_value(data, 'A')
['chain']

find_value(data, False)
['cable']
```

# Python 2.7 Usage

 - 2.7 does not guarantee ordering of dictionary's. If ordering matters, use [OrderedDict](https://docs.python.org/2.7/library/collections.html) for all dictionary's in the data.

# Web API

## Documentation

Visit [the swagger API documentation](https://jsonparse.dev/v1/docs)

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

### Brief Endpoint Overiew
```bash
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
POST /v1/value/{value}
```

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

```bash
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)

```python
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
```bash
pip install "jsonparse[webapi]"

gunicorn -b 0.0.0.0:8000 jsonparse.webapi:app
```

> Alternatively, run the docker container

```bash
docker run -d ctomkow/jsonparse
```
            

Raw data

            {
    "_id": null,
    "home_page": null,
    "name": "jsonparse",
    "maintainer": null,
    "docs_url": null,
    "requires_python": "!=3.0,!=3.1,!=3.2,!=3.3,!=3.4,!=3.5,>=2.7",
    "maintainer_email": "Craig Abt Tomkow <ctomkow@gmail.com>",
    "keywords": "JSON, filter, key value, parse, parsing, python, search, searching",
    "author": null,
    "author_email": "Craig Abt Tomkow <ctomkow@gmail.com>",
    "download_url": "https://files.pythonhosted.org/packages/f6/86/0cada90bd55822177ae0a2243e265f228528a695caacb0ef50d338c06bbf/jsonparse-0.15.1.tar.gz",
    "platform": null,
    "description": "# jsonparse: ctrl-f for json\n![PyPI - Python Version](https://img.shields.io/pypi/pyversions/jsonparse)\n![GitHub tag (latest SemVer)](https://img.shields.io/github/v/tag/ctomkow/jsonparse?label=version&sort=semver)\n[![jsonparse](https://github.com/ctomkow/jsonparse/actions/workflows/jsonparse-buildtest.yml/badge.svg)](https://github.com/ctomkow/jsonparse/actions/workflows/jsonparse-buildtest.yml)\n[![codecov](https://codecov.io/gh/ctomkow/jsonparse/branch/master/graph/badge.svg?token=affX7FZaFk)](https://codecov.io/gh/ctomkow/jsonparse)\n![Static Badge](https://img.shields.io/badge/powered_by-mead-E79A3F?style=flat)\n\n\n</br>\n\n> **jsonparse** is a simple JSON parsing library. Extract what's needed from key:value pairs.\n\n## What's New\n - Python 2.7 compat. :sweat_smile: :relieved:\n - A new function, [find_value](#find_value), has been added. This function will return all keys of the matched value. :grinning:\n - [CLI tool](#CLI-tool). Parse json text files or stdin via the command line :tada:\n\n# Python Library\n\n## Install\n```bash\npip install jsonparse\n```\n\n## Quickstart\nHere is a quick example of what jsonparse is able to do.\n\n```python\nfrom jsonparse import find_key, find_keys, find_key_chain, find_key_value, find_value\n\ndata = [{\n  \"key0\":\n    {\n      \"key1\": \"result\",\n      \"key2\":\n        {\n          \"key1\": \"result1\",\n          \"key3\": {\"key1\": \"result2\"}\n        }\n    }\n}]\n\nfind_key(data, 'key1')\n['result2', 'result1', 'result']\n\nfind_key_chain(data, ['key0', 'key2', 'key3', 'key1'])\n['result2']\n```\n\n:heavy_plus_sign: See additional documentation in the [API section](#API) below.\n\n\n# CLI tool\n\n## Install\n```bash\npip install jsonparse\n```\n\n## Quickstart\nSummary of cli commands. For complete information, `jp --help`\n\nNote, `jsonparse` and `jp` are equivalent.\n\n`jp key key1 --file text.json`\n\n`jp keys key1 key2 key3 --file text.json`\n\n`jp key-chain my '*' chain --file text.json`\n\n`jp key-value key1 '\"result\"' --file text.json`\n\n`echo '{\"key1\": {\"key2\": 5}}' | jp key key2`\n\n`jp value null --file text.json`\n\n`jp value 42 --file text.json`\n\n`jp value '\"strValue\"' --file text.json`\n\n\n# API\n\n- [jsonparse functions](#Functions)\n    - [find_key](#find_key)\n    - [find_keys](#find_keys)\n    - [find_key_chain](#find_key_chain)\n    - [find_key_value](#find_key_value)\n    - [find_value](#find_value)\n\nThe API examples using the following test data.\n\n```python\ndata = [\n    {\"key\": 1},\n    {\"key\": 2},\n    {\"my\": \n        {\"key\": \n            {\n                \"chain\": \"A\",\n                \"rope\": 5,\n                \"string\": 1.2,\n                \"cable\": False\n            }\n        }\n    },\n    {\"your\":\n    \t{\"key\":\n            {\n                \"chain\": \"B\",\n                \"rope\": 7,\n                \"string\": 0.7,\n                \"cable\": True\n            }\n    \t}\n    }\n]\n```\n\n---\n\n### Functions\n\n```python\nfrom jsonparse import find_key, find_keys, find_key_chain, find_key_value, find_value\n```\n\n---\n\n### find_key\n<pre>\n<b>find_key(</b><i>data</i>: dict | list | OrderedDict, <i>key</i>: str<b>)</b> -> list\n</pre>\n\n&nbsp;&nbsp;&nbsp;&nbsp;Will return all values of the matched key.\n\n```python\nfind_key(data, 'chain')\n['A', 'B']\n\nfind_key(data, 'key')\n[1, 2, {'chain': 'A', 'rope': 5, 'string': 1.2, 'cable': False}, {'chain': 'B', 'rope': 7, 'string': 0.7, 'cable': True}]\n```\n\n---\n\n### find_keys\n<pre>\n<b>find_keys(</b><i>data</i>: dict | list | OrderedDict, <i>keys</i>: list, <i>group</i>: bool = True<b>)</b> -> list\n</pre>\n\n&nbsp;&nbsp;&nbsp;&nbsp;The default return value is a two dimensional list. `[ [], [], ...]`.\n\n&nbsp;&nbsp;&nbsp;&nbsp;To return all values as a one dimensional list, set `group=False`.\n\n&nbsp;&nbsp;&nbsp;&nbsp;The ordering of the keys does not matter.\n\n```python\nfind_keys(data, ['rope', 'cable'])\n[[5, False], [7, True]]\n\nfind_keys(data, ['rope', 'cable'], group=False)\n[5, False, 7, True]\n```\n\n---\n\n### find_key_chain\n<pre>\n<b>find_key_chain(</b><i>data</i>: dict | list | OrderedDict, <i>keys</i>: list<b>)</b> -> list\n</pre>\n\n&nbsp;&nbsp;&nbsp;&nbsp;The key chain is an ordered list of keys. The chain needs to start at the root level of the nested data.\n\n&nbsp;&nbsp;&nbsp;&nbsp;Wildcard `*` can be used as key(s) to match any.\n\n```python\nfind_key_chain(data, ['my', 'key', 'chain'])\n['A']\n\nfind_key_chain(data, ['key'])\n[1, 2]\n\nfind_key_chain(data, ['*', 'key', 'chain'])\n['A', 'B']\n\nfind_key_chain(data, ['*', 'key', '*'])\n['A', 5, 1.2, False, 'B', 7, 0.7, True]\n```\n\n---\n\n### find_key_value\n<pre>\n<b>find_key_value(</b><i>data</i>: dict | list | OrderedDict, <i>key</i>: str, <i>value</i>: str | int | float | bool | None) -> list\n</pre>\n\n&nbsp;&nbsp;&nbsp;&nbsp;The returned list contains the dictionaries that contain the specified key:value pair.\n\n```python\nfind_key_value(data, 'cable', False)\n[{'chain': 'A', 'rope': 5, 'string': 1.2, 'cable': False}]\n\nfind_key_value(data, 'chain', 'B')\n[{'chain': 'B', 'rope': 7, 'string': 0.7, 'cable': True}]\n```\n\n---\n\n### find_value\n<pre>\n<b>find_value(</b><i>data</i>: dict | list | OrderedDict, <i>value</i>: str | int | float | bool | None<b>)</b> -> list\n</pre>\n\n&nbsp;&nbsp;&nbsp;&nbsp;Will return all keys of the matched value.\n\n```python\nfind_value(data, 'A')\n['chain']\n\nfind_value(data, False)\n['cable']\n```\n\n# Python 2.7 Usage\n\n - 2.7 does not guarantee ordering of dictionary's. If ordering matters, use [OrderedDict](https://docs.python.org/2.7/library/collections.html) for all dictionary's in the data.\n\n# Web API\n\n## Documentation\n\nVisit [the swagger API documentation](https://jsonparse.dev/v1/docs)\n\nAll endpoints are HTTP POST requests where you include the searchable JSON data in the request body.\n\n### Brief Endpoint Overiew\n```bash\nPOST /v1/key/{key}\nPOST /v1/keys?key=1&key=2&key=3&key=4...\nPOST /v1/keychain?key=1&key=2&key=3&key=4...\nPOST /v1/keyvalue?key=a&value=1\nPOST /v1/value/{value}\n```\n\n## Quickstart\nLet's practice using the public, free-to-use-no-authentication, web API hosted in GCP Cloud Run.\n\nWe are POST'ing the JSON data with curl, requesting to search for the key, 'key1'. The found key values are returned as JSON.\n\n```bash\ncurl -X POST \"https://jsonparse.dev/v1/key/key1\" \\\n-H 'Content-Type: application/json' \\\n-d '[{\"key0\":{\"key1\":\"result\",\"key2\":{\"key1\":\"result1\",\"key3\":{\"key1\":\"result2\"}}}}]'\n\n[\"result2\",\"result1\",\"result\"]\n```\n\n> OR (using python and requests library)\n\n```python\nimport requests\n\ndata = [{\n    \"key0\":\n    {\n        \"key1\": \"result\",\n        \"key2\":\n        {\n            \"key1\": \"result1\",\n            \"key3\": {\"key1\": \"result2\"}\n        }\n    }\n}]\n\nrequests.post('https://jsonparse.dev/v1/key/key1', json=data).json()\n\n['result2', 'result1', 'result']\n```\n\n## Self-Hosted\n```bash\npip install \"jsonparse[webapi]\"\n\ngunicorn -b 0.0.0.0:8000 jsonparse.webapi:app\n```\n\n> Alternatively, run the docker container\n\n```bash\ndocker run -d ctomkow/jsonparse\n```",
    "bugtrack_url": null,
    "license": null,
    "summary": "ctrl-f for JSON. A simple JSON parsing library. Extract what's needed from key:value pairs.",
    "version": "0.15.1",
    "project_urls": {
        "Documentation": "https://jsonparse.readthedocs.io/en/latest/",
        "Source code": "https://github.com/ctomkow/jsonparse"
    },
    "split_keywords": [
        "json",
        " filter",
        " key value",
        " parse",
        " parsing",
        " python",
        " search",
        " searching"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "e0d4e0e896b4d469e66bef78df6af3d15679d52ba448948bc07f9006faabf132",
                "md5": "365f02d8a4f0797a4303e57a3a3f7f0d",
                "sha256": "6dc6cab87ac2ef024fc96138310976e797c130103e8a0d174a7d5350d84c5096"
            },
            "downloads": -1,
            "filename": "jsonparse-0.15.1-py2.py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "365f02d8a4f0797a4303e57a3a3f7f0d",
            "packagetype": "bdist_wheel",
            "python_version": "py2.py3",
            "requires_python": "!=3.0,!=3.1,!=3.2,!=3.3,!=3.4,!=3.5,>=2.7",
            "size": 974324,
            "upload_time": "2024-10-12T03:35:18",
            "upload_time_iso_8601": "2024-10-12T03:35:18.720243Z",
            "url": "https://files.pythonhosted.org/packages/e0/d4/e0e896b4d469e66bef78df6af3d15679d52ba448948bc07f9006faabf132/jsonparse-0.15.1-py2.py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "f6860cada90bd55822177ae0a2243e265f228528a695caacb0ef50d338c06bbf",
                "md5": "6f0547b21fc57aaba263f94e2f6f3238",
                "sha256": "dbd944ac62e18c6c842a46ae026c33923ede8fcc34e27b0911541da68bb9dcaf"
            },
            "downloads": -1,
            "filename": "jsonparse-0.15.1.tar.gz",
            "has_sig": false,
            "md5_digest": "6f0547b21fc57aaba263f94e2f6f3238",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": "!=3.0,!=3.1,!=3.2,!=3.3,!=3.4,!=3.5,>=2.7",
            "size": 969129,
            "upload_time": "2024-10-12T03:35:21",
            "upload_time_iso_8601": "2024-10-12T03:35:21.054547Z",
            "url": "https://files.pythonhosted.org/packages/f6/86/0cada90bd55822177ae0a2243e265f228528a695caacb0ef50d338c06bbf/jsonparse-0.15.1.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-10-12 03:35:21",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "ctomkow",
    "github_project": "jsonparse",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "lcname": "jsonparse"
}
        
Elapsed time: 0.41459s