Extract specific keys from JSON objects with recursive search and wildcard matching
Project description
json_parser
A lightweight Python library for extracting specific keys from complex, nested JSON structures. Rather than manually traversing deeply nested objects, json_parser lets you declare the keys you care about and recursively retrieves their values — with support for wildcard matching and automatic merging of duplicate keys.
Features
- Key-based extraction — Specify the keys you need and let the parser handle the traversal.
- Wildcard matching — Use glob-style patterns (e.g.,
address*) to match multiple keys at once. - Recursive search — Automatically searches through nested dictionaries and lists.
- Duplicate key merging — When the same key appears at multiple levels, values are combined into a list.
- Zero dependencies — Uses only the Python standard library.
Installation
pip install json-key-parser
Or install from source:
git clone https://github.com/diverdale/json_parser.git
cd json_parser
pip install .
Quick Start
import json
from json_parser import JsonParser
json_data = [
{
"first_name": "John",
"last_name": "Doe",
"full_name": "John Doe",
"address1": {
"street": "1208 Elm Street",
"city": "Springfield",
"zip_code": "62704"
},
"address2": {
"street": "4965 Harvest Rd",
"city": "Springfield",
"zip_code": "62704"
},
"birthday": "1984-05-23"
},
{
"first_name": "Jane",
"last_name": "Smith",
"full_name": "Jane Smith",
"address": {
"street": "742 Evergreen Terrace",
"city": "Springfield",
"zip_code": "62701"
},
"birthday": "1990-11-12"
}
]
keys = ["first_name", "last_name", "birthday"]
result = JsonParser(json_data, keys).get_data()
print(json.dumps(result, indent=4))
Output:
[
{
"first_name": "John",
"last_name": "Doe",
"birthday": "1984-05-23"
},
{
"first_name": "Jane",
"last_name": "Smith",
"birthday": "1990-11-12"
}
]
Wildcard Matching
Use glob-style patterns to match keys dynamically. For example, address* matches address, address1, and address2:
keys = ["first_name", "address*", "birthday"]
result = JsonParser(json_data, keys).get_data()
Output:
[
{
"first_name": "John",
"address1": {
"street": "1208 Elm Street",
"city": "Springfield",
"zip_code": "62704"
},
"address2": {
"street": "4965 Harvest Rd",
"city": "Springfield",
"zip_code": "62704"
},
"birthday": "1984-05-23"
},
{
"first_name": "Jane",
"address": {
"street": "742 Evergreen Terrace",
"city": "Springfield",
"zip_code": "62701"
},
"birthday": "1990-11-12"
}
]
Duplicate Key Merging
When the same key is found at multiple nesting levels within a single record, the values are automatically combined into a list:
keys = ["first_name", "street", "birthday"]
result = JsonParser(json_data, keys).get_data()
Output:
[
{
"first_name": "John",
"street": [
"1208 Elm Street",
"4965 Harvest Rd"
],
"birthday": "1984-05-23"
},
{
"first_name": "Jane",
"street": "742 Evergreen Terrace",
"birthday": "1990-11-12"
}
]
API Reference
JsonParser(obj, args)
| Parameter | Type | Description |
|---|---|---|
obj |
list[dict] |
A list of JSON objects (dicts) to search. |
args |
list[str] |
Key names or glob patterns to extract. |
Raises JsonParserException if obj or args is empty.
Methods
| Method | Returns | Description |
|---|---|---|
get_data() |
list[dict] |
Extracts and returns matching key-value pairs from each record. |
get_json() |
list[dict] |
Returns the original JSON input. |
get_args() |
list[str] |
Returns the list of keys/patterns. |
Running Tests
pytest tests/
License
This project is licensed under the MIT License. See the LICENSE file for details.
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
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file json_key_parser-0.0.2.tar.gz.
File metadata
- Download URL: json_key_parser-0.0.2.tar.gz
- Upload date:
- Size: 3.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/2.3.2 CPython/3.11.11 Darwin/25.2.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5038776b95d42cfceece69accb9c156df4b10586ef58442dd722919647629cc1
|
|
| MD5 |
f7d0a42e5d1041957916c7b6d07c29a1
|
|
| BLAKE2b-256 |
5aa8bb2b22a629bf888f66593371ac650f121516e5dd668f36035451c1679d7b
|
File details
Details for the file json_key_parser-0.0.2-py3-none-any.whl.
File metadata
- Download URL: json_key_parser-0.0.2-py3-none-any.whl
- Upload date:
- Size: 4.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/2.3.2 CPython/3.11.11 Darwin/25.2.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7953d57005db44dbdb7cc29cb6998609898bdf00e78231373a3a11c1c5b16be6
|
|
| MD5 |
13c6f598e0e72842d63cc8705150a197
|
|
| BLAKE2b-256 |
112fdb8e4d902c7b5ee4148ca8ab174565a3ac7b566774261caccd3611faa29f
|