Search through JSON data key:values
Project description
jsonparse: ctrl-f for json
jsonparse is a simple JSON parsing library. Extract what's needed from key:value pairs.
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}]
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for jsonparse-0.11.0.dev1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | dad139defedbb644ca0997ffe632e645a7258c3ccc0d4f9e8d9f4a8d3ccbb6bd |
|
MD5 | 5af1a5d9f797916d446df7c133053297 |
|
BLAKE2b-256 | fc8744d4b98a650a3e9c063cbcac5e097284d8db742769dec3e40bc3db7f29fd |