Bisoux
Bisoux

Reputation: 522

Summing up values in jinja2

What would be the right way of getting the sum of the value in details.sections.details filtered by another attribute which would be the the value in name, i.e.: Earnings / Deduction?

Given the data:

{
   "details":{
      "totalPay":482.66,
      "currency":"OMR",
      "contributions":48.24,
      "sections":[
         {
            "name":"Earnings",
            "value":519.41,
            "details":[
               {
                  "name":"Overtime",
                  "value":60,
                  "notes":"Border OT",
                  "currency":"OMR"
               },
               {
                  "name":"H R A",
                  "value":96,
                  "notes":"",
                  "currency":"OMR"
               },
               {
                  "name":"LIVING ALLOWANCE",
                  "value":32,
                  "notes":"",
                  "currency":"OMR"
               },
               {
                  "name":"T A",
                  "value":32,
                  "notes":"",
                  "currency":"OMR"
               },
               {
                  "name":"Basic salary",
                  "value":299.408,
                  "notes":"",
                  "currency":"OMR"
               }
            ]
         },
         {
            "name":"Deductions",
            "value":36.75,
            "details":[
               {
                  "name":"PASI - Employee (7%) - Employee",
                  "value":32.1586,
                  "notes":null,
                  "currency":"OMR"
               },
               {
                  "name":"Social Security Recovery (1%) - Employee",
                  "value":4.5941,
                  "notes":null,
                  "currency":"OMR"
               }
            ]
         }
      ]
   }
}

I would expect to get, for Earnings:

60 + 96 + 32 + 32 + 299.408 = 519.408

and, for the Deductions:

32.1586 + 4.5941 = 36.7527

I will then dispalay the summed up values in a table.

I tried

{% set sum =  details.sections.details|sum(attribute='value')  -%}

But, the script does not load and I think there is an error in it.


I am able to get the individual values by looping.
I have declared a variable:

{% set sumAdditions = 0 %}    
{% sumAdditions  = sumAdditions + details.value %}

but this seems to fails as the script errors out.

Upvotes: 2

Views: 700

Answers (1)

β.εηοιτ.βε
β.εηοιτ.βε

Reputation: 39159

details.sections is a list, so you either have to filter the list for earnings and deductions:

{% set earnings = (
     details.sections | selectattr('name', 'eq', 'Earnings') | first
   ).details | sum(attribute='value')
%}
{% set deductions = (
     details.sections | selectattr('name', 'eq', 'Deductions') | first
   ).details | sum(attribute='value')
%}

Or do it dynamically in a loop:

{% set sums = {} %}
{% for section in details.sections %}
  {% do sums.update({
       section.name: section.details | sum(attribute='value') 
     }) 
  %}
{% endfor %}

Where sums will now contain a dictionary:

{'Earnings': 519.408, 'Deductions': 36.7527}

If, for some reason the {% do %} construct is not available for you, you can use a dummy set, so assign the return of the dictionary update to a variable you won't use:

{% set sums = {} %}
{% for section in details.sections %}
  {% set dummy = sums.update({
       section.name: section.details | sum(attribute='value') 
     }) 
  %}
{% endfor %}

Upvotes: 2

Related Questions