From this json:
{
"obj": [
{
"int": 0
},
{
"int": 1
}
]
}
I would like to obtain the list of int
property of the field obj
so I can do an aggregation on the list, e.g. taking the mean of int
in obj
, using Jinja2 syntax.
I have tried using the filter selectattr("int")
combined with the sum
filter but I get `unsupported operand type(s) for +: 'int' and 'dict'.
I am using Jinja2 with docxtpl
to generate docx file from template.
import json
j = '{"obj": [{"int": 0},{"int": 1}]}'
context = json.loads(j)
from docxtpl import DocxTemplate
import jinja2
tpl = DocXtemplate('template.docx')
tpl.render(context, jinja_env)
tpl.save('out.docx')
The docx template contains the single line:
{{obj|selectattr("int")|sum}}
I would like to see 1
in the doxc file when taking the sum as I tried to do just above, I would also like to be able to do the mean and eventually reuse it in other operations.
EDIT: As an answer to those saying I should do it inside python:
I am developing a tool for generating docx reports from a database and a template file. The first part of the tool is a C++ program that generates json data from the database. The second part is a script for generating the report in docx format from a template using Jinja2. I would prefer the user only to mess with the template and not having to modify the script, also I would like to have some modularity and be able to compute aggregations over the json tree. Since Jinja2 is providing sum
on list and other sort of operations, I thought it could add another layer of modularity without me having to write more code. At the moment I already added the possibility for the user to do aggregationS on the json tree and insert it into the node tree, but my solution is not perfect and it comes at the cost of providing another template file for data generation and it makes the tool more complex.
I hope it makes more sense now why I don't want to do it in the script.
Edit2: A slightly more complex example:
{
"arr": [
{
"obj": [
{
"name": 0
},
{
"name": 1
}
]
},
{
"obj": [
{
"name": 0
},
{
"name": 1
}
]
}
]
}
I would like to be able to do something like:
{{arr|get_attr_list('obj')|get_attr_list('int')}}
An even better solution would be able to do list extension in Jinja2 directly:
{{ [ y[int] for y in x[obj] for x in arr ] | sum }}