How to generate json using Jinja2 Template Engine

26,950

Solution 1

Manually constructing json runs the risk of accidentally producing an invalid json string. It's safer to use a tool to do this, and it keeps your templates less cluttered.

If you are using Jinja 2.9 or later, you can use the built-in tojson filter to convert Python objects* to json automatically.

>>> import pprint

>>> # Construct some test data
>>> matches = ['1', '2', '3']
>>> times = ['19:00', '21:00', '23:00']
>>> teams = ['Team 1 - Team 2', 'Team 3 - Team 4', 'Team 5 - Team 6']

>>> # Combine the data structures to match the required output
>>> match_data = [dict(zip(['time', 'team'], pair)) for pair in zip(times, teams)]
>>> combined = {x: y for x, y in zip(matches, match_data)}
>>> pprint.pprint(combined)
{'1': {'team': 'Team 1 - Team 2', 'time': '19:00'},
 '2': {'team': 'Team 3 - Team 4', 'time': '21:00'},
 '3': {'team': 'Team 5 - Team 6', 'time': '23:00'}}

>>> template = jinja2.Template("""{{ matches | tojson(indent=2) }}""")
>>> print(template.render(matches=combined))
{
  "1": {
    "team": "Team 1 - Team 2",
    "time": "19:00"
  },
  "2": {
    "team": "Team 3 - Team 4",
    "time": "21:00"
  },
  "3": {
    "team": "Team 5 - Team 6",
    "time": "23:00"
  }
}

For earlier Jinja releases, construct the json using the json package in Python's standard library, and then render the json in your template.

>>> import json    
>>> # Serialise our object as json; setting the indent argument gives
>>> # the pretty printed format that we want.
>>> jdata = json.dumps(combined,  indent=2) 
>>> print(jdata)
{
  "1": {
    "time": "19:00",
    "team": "Team 1 - Team 2"
  },
  "2": {
    "time": "21:00",
    "team": "Team 3 - Team 4"
  },
  "3": {
    "time": "23:00",
    "team": "Team 5 - Team 6"
  }
}

>>> # Pass the json to the template for rendering.
>>> template = jinja2.Template("""{{ matches }}""")
>>> print(template.render(matches=jdata))
{
  "1": {
    "time": "19:00",
    "team": "Team 1 - Team 2"
  },
  "2": {
    "time": "21:00",
    "team": "Team 3 - Team 4"
  },
  "3": {
    "time": "23:00",
    "team": "Team 5 - Team 6"
  }
}

* The filter only handles primitives AFAICT; if your data includes datetimes for example you will need to stringify them first.

Solution 2

I found a Gist and forked it to make trivial changes for python 3.x compatibility and it worked perfectly for me:

https://gist.github.com/jerrydboonstra/bedb5ccbe6200b5d794447ed38aeb342

It has an example.json file that acts as the template with "{{ variable }}" tokens, and a runner.py which composes the output from the template and an input data structure.

enter image description here

Share:
26,950
hullabaloon
Author by

hullabaloon

Updated on December 13, 2021

Comments

  • hullabaloon
    hullabaloon over 2 years

    I am trying to generate a json file from a .xlsx file.

    so far I was able to get data from the file but I am not sure how to put them on json using jinja2. There is a problem in template structure. How can I solve this?

    Output should be like this

      "Matches": {    
        "1": {
          "time": "19:00",
          "teams": "Team 1 - Team 2"
        },
        "2": {
          "time": "21:00",
          "teams": "Team 3 - Team 4"
        },
        ...
        ...
        ...
      }
    

    My code is something like this. Obviously template section is wrong.

    from openpyxl import load_workbook
    from jinja2 import Template
    
    
    start_coloumn_of_matches = 3
    end_coloumn_of_matches = 20
    
    wb = load_workbook(filename = 'myfile.xlsx')
    sheet_ranges = wb['Sheet1']
    
    keys = []
    teams = []
    times = []
    
    
    for x in range(start_coloumn_of_matches, end_coloumn_of_matches + 1):
        team_column = 'A' + str(x)
        time_column = 'D' + str(x)
        teams.append(sheet_ranges[team_column].value)
        times.append(sheet_ranges[time_column].value)
        keys.append(x)
    
    
    
    template = Template(''' 
    "Matches": {
            {% for key in keys %}
          "{{key}}": 
            {% endfor %}
          {
            {% for team in teams %}
            "teams": "{{team}}",
            {% endfor %}
            {% for time in times %}
            "time": "{{time}}"
            {% endfor %}
            }
    
        }, 
    
        ''' )
    
    print(template.render(teams = teams, times = times, keys = keys))
    
  • Uri
    Uri over 5 years
    plus one for the first two sentences. Better not using jinja2 for generating json.
  • Gert van den Berg
    Gert van den Berg about 4 years
    @Uri Except that it does not help in, say, Ansible, where direct python is not an option...