Giter Site home page Giter Site logo

vlazaroes / jsoninja Goto Github PK

View Code? Open in Web Editor NEW
5.0 1.0 0.0 25 KB

🥷 Jsoninja is a library that allows you to generate JSON's from templates written with Python data types.

Home Page: https://pypi.org/project/jsoninja

License: MIT License

Python 97.87% Shell 2.13%
json python templates

jsoninja's Introduction

Jsoninja

A library that allows you to generate JSON's from
templates written with Python data types.

Library version Supported Python versions

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.

jsoninja's People

Contributors

vlazaroes avatar

Stargazers

 avatar  avatar Jordi Ros Mayor avatar Javi Palacios avatar José Manuel Sirvent avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.