Skip to main content

A small python module for populating json template files.

Project description

json-templates

A small python module for populating json template files.

Version 0.1.0

Accepts either a json string or a file path and a dictionary. json-template replaces the place holders found in the json with those found in the dictionary.

Below is a template example

{
  "key":"hard coded value",
  "key2":1,
  "key3":"{{ variable }}",
  "key4":"{% array %}"
}

Currently supports {{ variable }} for single replacement and {% array %} for iterable replacement.

For example given the following dictionary

{
  "variable":"hello world",
  "array":["foo","bar"]
}

The JSON would become

{
  "key":"hard coded value",
  "key2":1,
  "key3":"hello world",
  "key4":["foo","bar"]
}

Usage

import JsonTemplates

json_tmp = JsonTemplates()
result = json_tmp.load("template.json")

if result[0]:
  new_dict = json_tmp.generate({"variable":"hello world","array":["foo","bar"]})

Methods

  • load(json_file_path) - Loads a JSON file
    Returns a tuple (Success,error message or dictionary)

  • loads(json_str) - Loads a JSON string
    Returns a tuple (Success,error message or dictionary)

  • generate(replacement_dict) - Takes in a dictionary of replacement values and generates a new dictionary with the placeholders replaced with the values in the dictionary
    Returns a tuple (Success, error message or dictionary)

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

jsontemplates-0.1.0.tar.gz (4.0 kB view hashes)

Uploaded Source

Built Distribution

jsontemplates-0.1.0-py3-none-any.whl (3.7 kB view hashes)

Uploaded Python 3

Supported by

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