Skip to main content

JSON to Func

A Python library that converts JSON keys to callable methods on a class.

Installation

pip install json-to-func

Usage

Basic Usage

from json_to_func import JsonToFunc, load

# Load from a JSON file
translator = JsonToFunc("messages.json")

# Or use the convenience function
translator = load("messages.json")

# Or load from a dictionary
translator = JsonToFunc({
    "hello": "Hello, World!",
    "greeting": "Hello, %s!"
})

Calling Methods

JSON keys are automatically transformed into valid Python method names:

# Simple key
translator.hello()  # Returns: "Hello, World!"

# With placeholder
translator.greeting("John")  # Returns: "Hello, John!"

Key Transformation Rules

Original Key Method Name
tomans tomans()
field-required field_required()
hint:title hint__title()
hint-bar:text:message hint_bar__text__message()

Rules:

  • Dashes (-) become single underscores (_)
  • Colons (:) become double underscores (__)
  • Other invalid characters become underscores
  • Keys starting with numbers get a leading underscore

Format String Support

The library supports %s placeholders in values:

data = {
    "field-required": "%s is required",
    "range-error": "Value must be between %s and %s"
}

translator = JsonToFunc(data)

translator.field_required("Email")  # "Email is required"
translator.range_error("1", "10")   # "Value must be between 1 and 10"

Alternative Access Methods

# Get by original key name
translator.get("field-required", "Email")

# Get by transformed function name
translator.get_by_func_name("field_required", "Email")

# With default value
translator.get("nonexistent", default="fallback")

Utility Methods

# List all original keys
translator.keys()  # ["hello", "field-required", ...]

# List all method names
translator.func_names()  # ["hello", "field_required", ...]

# Check if key exists
"hello" in translator  # True
"field_required" in translator  # True

# Get count
len(translator)  # Number of entries

Example with i18n/Translation Files

{
  "tomans": "تومان",
  "field-required": "%s الزامی است",
  "entity-not-exists": "چنین %s وجود ندارد",
  "hint-bar:title:welcome": "خوش آمدید"
}
from json_to_func import JsonToFunc

messages = JsonToFunc("fa.json")

print(messages.tomans())  # تومان
print(messages.field_required("ایمیل"))  # ایمیل الزامی است
print(messages.entity_not_exists("کاربر"))  # چنین کاربر وجود ندارد
print(messages.hint_bar__title__welcome())  # خوش آمدید

License

MIT

Metadata

Release files for json-to-func 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for json-to-func 0.1.0
File Size Uploaded
json_to_func-0.1.0.tar.gz 6.7 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for json-to-func 0.1.0
File Interpreter ABI Platform
json_to_func-0.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 11.7 kB

Release files / json_to_func-0.1.0.tar.gz

Download URL json_to_func-0.1.0.tar.gz
Size 6.7 kB
Tags Source
SHA-256 checksum
How to use checksums
e8056432e97709771338184712e78d02c602f699356194dd3cf898b33e3fe1d4
BLAKE2b-256 checksum
How to use checksums
a3e75f54047fc1a1c657d9fa6a7b3c14862ad8f3b38fc1333c0c83caba32bde8
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.13.5

Release files / json_to_func-0.1.0-py3-none-any.whl

Download URL json_to_func-0.1.0-py3-none-any.whl
Size 5.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
11c4452c3da50bfb55d0b9b0f315fad63a6a3adb8e3c70accfce87397daa0465
BLAKE2b-256 checksum
How to use checksums
412512ec71dac9c6589260a58e7d32d9ba7a88836378feff56f820792bced4b8
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.13.5

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page