Skip to main content

Extract specific keys from JSON objects with recursive search and wildcard matching

Project description

json_parser

License: MIT Python 3.8+

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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

json_key_parser-0.0.1.tar.gz (3.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

json_key_parser-0.0.1-py3-none-any.whl (4.2 kB view details)

Uploaded Python 3

File details

Details for the file json_key_parser-0.0.1.tar.gz.

File metadata

  • Download URL: json_key_parser-0.0.1.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

Hashes for json_key_parser-0.0.1.tar.gz
Algorithm Hash digest
SHA256 46a0631ed8d2b7cef27f135b88fb504859609f39d3778a82ddffa239bf898b65
MD5 f696985427b4505f69289e8ad0399849
BLAKE2b-256 9f7c8d57dc15fbf7623460d0d7adcb6cbd895f50cde5535eccea45d125627295

See more details on using hashes here.

File details

Details for the file json_key_parser-0.0.1-py3-none-any.whl.

File metadata

  • Download URL: json_key_parser-0.0.1-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

Hashes for json_key_parser-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 6049927dff901276346c967bd10efef16fd43508399ae1376f705e28930d6a34
MD5 bc14dbd2fc2bbab19644cc9173050e7b
BLAKE2b-256 a093d7fbe2f3e079572842a2bdfc86307cf13c36937b6f14c05e1da1dcb376cf

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page