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))
Answer
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.