How to convert string to list and iterate in jinja2

I have made tax breakup into list of dict

[{"Item Name":"Beakers, Griffin, Low Form , with spout 100ml BOROSIL Cat. No. 1000D16","Taxable Amount":"54.60","SGST (9%) - DL":"(9.0%) 4.91","CGST (9%) - DL":"(9.0%) 4.91"},
{"Item Name":"Beakers, Griffin, Low Form , with spout 250ml BOROSIL Cat. No. 1000D21","Taxable Amount":"280.80","SGST (9%) - DL":"(9.0%) 25.27","CGST (9%) - DL":"(9.0%) 25.27"},
{"Item Name":"1000 ml Glass Beakers 1000D29 Borosil","Taxable Amount":"758.16","SGST (9%) - DL":"(9.0%) 68.23","CGST (9%) - DL":"(9.0%) 68.23"},{"Item Name":"Flasks, Erlenmeyer, Conical, Narrow Mouth 100ml BOROSIL 4980016","Taxable Amount":"720.72","SGST (9%) - DL":"(9.0%) 64.86","CGST (9%) - DL":"(9.0%) 64.86"},{"Item Name":"Flasks, Erlenmeyer, Conical, Narrow Mouth 500ml BOROSIL 4980024","Taxable Amount":"1,591.20","SGST (9%) - DL":"(9.0%) 143.21","CGST (9%) - DL":"(9.0%) 143.21"},{"Item Name":"Glass reagent Bottle 250 ml Borosil 1501021","Taxable Amount":"951.60","SGST (9%) - DL":"(9.0%) 85.64","CGST (9%) - DL":"(9.0%) 85.64"},{"Item Name":"Bottles, Reagent, Graduated With Screw Cap and Pouring Ring Borosil 1501024","Taxable Amount":"772.20","SGST (9%) - DL":"(9.0%) 69.50","CGST (9%) - DL":"(9.0%) 69.50"},{"Item Name":"Bottles, Reagent, Graduated With Screw Cap and Pouring Ring BOROSIL 1501038 ","Taxable Amount":"13,713.96","SGST (9%) - DL":"(9.0%) 1,234.26","CGST (9%) - DL":"(9.0%) 1,234.26"},{"Item Name":"Measuring Cylinder  BOROSIL 3022006 - 10 ML","Taxable Amount":"400.92","SGST (9%) - DL":"(9.0%) 36.08","CGST (9%) - DL":"(9.0%) 36.08"},{"Item Name":"Measuring Cylinder  BOROSIL 3022012 - 50 ML","Taxable Amount":"514.80","SGST (9%) - DL":"(9.0%) 46.33","CGST (9%) - DL":"(9.0%) 46.33"},{"Item Name":"Measuring cylinder, Graduated, Single Metric Scale, with Pour out, with Hexagonal Base, Class B 250ml BOROSIL 3022021","Taxable Amount":"1,090.44","SGST (9%) - DL":"(9.0%) 98.14","CGST (9%) - DL":"(9.0%) 98.14"},{"Item Name":"Measuring Cylinder 1000ml BOROSIL 3022029","Taxable Amount":"2,157.48","SGST (9%) - DL":"(9.0%) 194.17","CGST (9%) - DL":"(9.0%) 194.17"}]

How I can iterate this in jinja2?

I have tried json and list filter in jinja2, but its not working

Something like:

{% for row in item_taxes %}
   {{ row['Item Name'] }} {{ row['Taxable Amount'] }}
{% endfor %}

?

@rmehta

No, it will not work, as it will treat as string.
I checked solution and find that we need to write some custom filter.

Also frappe custom json filter return string with leading slash. Should I send pull request to add new filter dict, json and list in jinja.

Please see following image.

Finally I have made GST format as per some most common requirement.

For this, we need to add json filter in frappe.
I have created github issue for this.
https://github.com/frappe/frappe/issues/3928

Any news on this?

Have you figured this out? in v11, passing object to jinja seems not to be possible anymore. So we are planning to pass a json string instead and have it parsed in jinja template. We are doing this in script report by the way.

Maybe this helps (edited):

ah thanks! will try

Hello Team,

Look what I did.

utils.py

import frappe
import json

@frappe.whitelist()
def string_to_json(json_string):
    return json.loads(json_string)

hooks.py

jenv = {
    "methods": [
        "string_to_json:path.to.your.function.string_to_json"
    ]
}

jinja template

<pre>
{{ string_to_json(json_string) | json }}
</pre>

Everything is done.

Good Luck!

4 Likes

Hi what does jenv represent and how does frappe interpret it in jinja?