Skip to main content

Enhanced JSON processing with includes, comments, and more

Project description

pypaya-json

Enhanced JSON processing with includes, comments, and more.

Features

  • File Inclusions: Include other JSON files using "include" declarations
  • Comment Support: Add comments to JSON files using custom comment characters
  • Flexible Configuration: Use as class methods for one-time operations or instances for reusable configurations
  • Nested Key Access: Navigate and extract data from nested JSON structures
  • Conditional Processing: Enable/disable sections using custom enable keys
  • Value Replacement: Replace entire sections with data from external files

Installation

pip install pypaya-json

Quick start

One-time usage (class method)

from pypaya_json import PypayaJSON

# Basic loading
data = PypayaJSON.load("config.json")

# With comments support
data = PypayaJSON.load("config.json", comment_string="#")

# With custom enable key
data = PypayaJSON.load("config.json", enable_key="active")

Reusable configuration (instance)

from pypaya_json import PypayaJSON

# Create a reusable loader
loader = PypayaJSON(enable_key="active", comment_string="//")

# Load multiple files with same settings
config = loader.load_file("config.json")
settings = loader.load_file("settings.json")

Examples

Basic file inclusion

main.json:

{
  "app_name": "MyApp",
  "include": {
    "filename": "database.json"
  },
  "features": ["auth", "api"]
}

database.json:

{
  "host": "localhost",
  "port": 5432,
  "name": "myapp_db"
}

Result:

data = PypayaJSON.load("main.json")
# {
#   "app_name": "MyApp",
#   "host": "localhost",
#   "port": 5432,
#   "name": "myapp_db",
#   "features": ["auth", "api"]
# }

Comments support

config.json:

{
  "server": {
    "host": "0.0.0.0",    // Bind to all interfaces
    "port": 8080          // Default port
  },
  // "debug": true,       // Commented out
  "workers": 4
}
data = PypayaJSON.load("config.json", comment_string="//")
# Comments are automatically stripped

Nested key access

data.json:

{
  "database": {
    "connections": {
      "primary": "postgresql://...",
      "replica": "postgresql://..."
    }
  }
}

main.json:

{
  "include": {
    "filename": "data.json",
    "keys_path": "database/connections/primary"
  }
}

Result: "postgresql://..."

Conditional inclusion

{
  "base_config": "value",
  "include": {
    "filename": "optional.json",
    "enabled": false
  }
}
# With custom enable key
loader = PypayaJSON(enable_key="active")
data = loader.load_file("config.json")

Value replacement

{
  "database": {
    "replace_value": {
      "filename": "secrets.json",
      "key": "database_url"
    }
  }
}

API Reference

PypayaJSON class

Class methods

  • PypayaJSON.load(path, enable_key="enabled", comment_char=None) - Load JSON file with one-time configuration

Instance methods

  • PypayaJSON(enable_key="enabled", comment_char=None) - Create reusable loader instance
  • loader.load_file(path) - Load JSON file using instance configuration

Parameters

  • path (str): Path to the JSON file
  • enable_key (str): Key used for conditional inclusion (default: "enabled")
  • comment_char (str, optional): Character that denotes comments (default: None)

Advanced usage

Multiple inclusions

{
  "include": [
    {"filename": "config1.json"},
    {"filename": "config2.json", "keys": ["specific_key"]},
    {"filename": "config3.json", "enabled": false}
  ]
}

Specific key selection

{
  "include": {
    "filename": "large_config.json",
    "keys": ["database", "cache", "logging"]
  }
}

Deep nested access

{
  "include": {
    "filename": "nested.json",
    "keys_path": ["level1", "level2", "target_key"]
  }
}

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

License

MIT License - see 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

pypaya_json-0.2.1.tar.gz (5.1 kB view details)

Uploaded Source

Built Distribution

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

pypaya_json-0.2.1-py3-none-any.whl (5.5 kB view details)

Uploaded Python 3

File details

Details for the file pypaya_json-0.2.1.tar.gz.

File metadata

  • Download URL: pypaya_json-0.2.1.tar.gz
  • Upload date:
  • Size: 5.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.11

File hashes

Hashes for pypaya_json-0.2.1.tar.gz
Algorithm Hash digest
SHA256 a03a0723e8d53184baf059cc3a9d9adb4b7f2ff45ea5c725e0a55c4f205a1f3c
MD5 d55515de516e727ce220dbcb4376634e
BLAKE2b-256 d41e870a339e86dd0eb465d7d9c96394be4b61b8d03fb65509301beb3adce5ea

See more details on using hashes here.

File details

Details for the file pypaya_json-0.2.1-py3-none-any.whl.

File metadata

  • Download URL: pypaya_json-0.2.1-py3-none-any.whl
  • Upload date:
  • Size: 5.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.11

File hashes

Hashes for pypaya_json-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 15af6acecf2803ea880e7142a926c1baaad989241ea3bcbaf8ec14b8b922f5a6
MD5 8501d4de186ac410d9a485417047d8fb
BLAKE2b-256 82b52713e213bda3c13d8534988bbe1e911162427eb411afaf68fe2639b36ea0

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