Jsoninja is a library that allows you to generate JSON's from templates written with Python data types.
Project description
Jsoninja
A library that allows you to generate JSON's from
templates written with Python data types.
Instalation
$ pip install jsoninja
Use Case Examples
You can use the main Python data types:
from jsoninja import Jsoninja
jsoninja = Jsoninja()
template = {
"foo": "{{variable_name}}",
}
replacements = {
"variable_name": "bar",
}
result = jsoninja.replace(template, replacements)
# {
# "foo": "bar",
# }
Allows multiple replacements of the same variable:
from jsoninja import Jsoninja
jsoninja = Jsoninja()
template = {
"message1": "{{message}}",
"message2": "{{message}}",
"message3": "{{message}}",
}
replacements = {
"message": "I am duplicated!",
}
result = jsoninja.replace(template, replacements)
# {
# "message1": "I am duplicated!",
# "message2": "I am duplicated!",
# "message3": "I am duplicated!",
# }
Use callback functions to generate the values to be replaced:
from jsoninja import Jsoninja
def generate_password() -> str:
return "super_secret_password"
jsoninja = Jsoninja()
template = {
"password": "{{password}}",
}
replacements = {
"password": generate_password,
}
result = jsoninja.replace(template, replacements)
# {
# "password": "super_secret_password",
# }
Support for variables in the dict keys (replacements must be str, int, float or bool):
from jsoninja import Jsoninja
jsoninja = Jsoninja()
template = {
"{{variable_name}}": "bar",
}
replacements = {
"variable_name": "foo",
}
result = jsoninja.replace(template, replacements)
# {
# "foo": "bar",
# }
Known Limitations
- When replacing a variable declared in a
dict
key, the order will change.
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
jsoninja-1.0.2.tar.gz
(4.5 kB
view hashes)