Forum Discussion

Jamesvan_der_Wa's avatar
Jamesvan_der_Wa
Qrew Trainee
12 months ago

mixing jinja with to_json pipe

Hi all 

I'm trying to get the following json object to be assembled during a pipeline:

{{{
   "service":"CreateOrder",
   "site_Acc_id":"gausolar",
   "order_source":"GoSolr Test",
   "fname":a.name,
   "lname":a.surname,
   "email":a.email,
   "mobile":a.mobile_number,
   "wnumber":a.mobile_number,
   "email":a.email,
   "unit_id":"",
   "street_num":"",
   "street_name":a.premise_address,
   "region":a.area_of_install,
   "city":"",
   "suburb":"",
   "service_array":[
      {
         "supplier_sku":a.system_size_sku,
         "roof_type":a.roof_type,
         "flat_roof":a.is_flat_roof
      }
   ],
   "instl_address":a.premise_address,
   "postalcode":"",
   "promocode":"",
   "sales_stage":"To Be Scheduled",
   "teamid":0,
   "coc_certificate":"No",
   "inst_start_date_time":"",
   "inst_end_date_time":"",
   "completion_date1":"",
   "inst_turnaround":"",
   "order_no":a.order,
   "order_size":a.system_size_sku,
   "prepaid_postpaid": a.prepaid_meter,
   "elec_provider":a.electricity_provider,
   "roof_type":a.roof_type,
   "comment_1":a.scheduling_comment,
   "inst_size":"",
   "income":"",
   "upgrade":"",
   "panels":"",
   "battery":"No",
   "flat_roof":"",
   "extra":"",
   "expense":"",
   "profit":"",
   "o_m_fee":"65.00",
   "xtra_battery":"",
   "has_flat_roof":a.is_flat_roof,
   "own_or_rent":"",
   "meter_type":"",
   "mnth_elec_bill":"",
   "elec_phases":{% if a.electricity_phase == "Single" %}
  "Single Phase"
{% elif a.electricity_phase == "Three" %}
  "Three Phase"
{% else %}
  "I Dont Know"
{% endif %} ,
   "owner_insurance":"",
   "dwelling_type":"",
   "gosolr_pass":""
}|to_json
}}

As you can see I'm piping the whole  object to_json at the end, sadly this is causing the jinja  if statements to give issues. Looks like you can't blanket parse the whole object if there are jinja statements in between. 

The main issue is, if I to_json each field independently, then the payload doesn't format correctly and the end point API rejects the call.

How to I still use to_json at the end while still allowing jinja if statements in between?



------------------------------
James van der Walt
------------------------------

3 Replies

  • DwightMunson1's avatar
    DwightMunson1
    Qrew Assistant Captain

    Could you set it as a variable and then send it to JSON at the end? I did something similar before. 

    {% set jsonPayload = {
      "service": "CreateOrder",
      "site_Acc_id": "gausolar",
      "order_source": "GoSolr Test",
      "fname": "{{ a.name }}",
      "lname": "{{ a.surname }}",
      "email": "{{ a.email }}",
      "mobile": "{{ a.mobile_number }}",
      "wnumber": "{{ a.mobile_number }}",
      "email": "{{ a.email }}",
      "unit_id": "",
      "street_num": "",
      "street_name": "{{ a.premise_address }}",
      "region": "{{ a.area_of_install }}",
      "city": "",
      "suburb": "",
      "service_array": [
        {
          "supplier_sku": "{{ a.system_size_sku }}",
          "roof_type": "{{ a.roof_type }}",
          "flat_roof": "{{ a.is_flat_roof }}"
        }
      ],
      "instl_address": "{{ a.premise_address }}",
      "postalcode": "",
      "promocode": "",
      "sales_stage": "To Be Scheduled",
      "teamid": 0,
      "coc_certificate": "No",
      "inst_start_date_time": "",
      "inst_end_date_time": "",
      "completion_date1": "",
      "inst_turnaround": "",
      "order_no": "{{ a.order }}",
      "order_size": "{{ a.system_size_sku }}",
      "prepaid_postpaid": "{{ a.prepaid_meter }}",
      "elec_provider": "{{ a.electricity_provider }}",
      "roof_type": "{{ a.roof_type }}",
      "comment_1": "{{ a.scheduling_comment }}",
      "inst_size": "",
      "income": "",
      "upgrade": "",
      "panels": "",
      "battery": "No",
      "flat_roof": "",
      "extra": "",
      "expense": "",
      "profit": "",
      "o_m_fee": "65.00",
      "xtra_battery": "",
      "has_flat_roof": "{{ a.is_flat_roof }}",
      "own_or_rent": "",
      "meter_type": "",
      "mnth_elec_bill": "",
      "elec_phases": "{% if a.electricity_phase == 'Single' %}Single Phase{% elif a.electricity_phase == 'Three' %}Three Phase{% else %}I Dont Know{% endif %}",
      "owner_insurance": "",
      "dwelling_type": "",
      "gosolr_pass": ""
    } %}

    {{ jsonPayload|to_json }}



    ------------------------------
    Dwight Munson
    ------------------------------
    • Jamesvan_der_Wa's avatar
      Jamesvan_der_Wa
      Qrew Trainee

      Thanks for the quick reply Dwight, 
      Sadly the solution didn't work.
      First, I see you put quotations around all the variables. If I do that then all of these are just strings in the final output.
      Second, If I remove the quotation then the body section of the "Make Request" doesn't validate.   



      ------------------------------
      James van der Walt
      ------------------------------
      • DwightMunson1's avatar
        DwightMunson1
        Qrew Assistant Captain

        Maybe it has to do with the white space around the jinja expressions? I should have taken that out. 

        This is what I do with mine, in which I query records in a work request table to find work to charge project management time to. That data is the b.json.data. Then I use this step to loop through the data and create time cards up to the time in a.hours.

        Make a request step:
        Method: POST

        Content Type: Application/json
        Body: 

        {
        {% set myDataSet = "myDBID" %}
        {% set myTimeStop = namespace(val=a.hours) %}
        {% set runningTotal = namespace(val=0.0) %}
        {% set relatedTimeSheet = namespace(val=a.related_time_sheet) %}
        {% set relatedWorkCode = namespace(val=a.related_work_code) %}
        {% set relatedClass = namespace(val=a.related_class) %}
         
        "to": "{{myDataSet}}",
        "data":[
            {% for row in (b.json.data) if runningTotal.val < myTimeStop.val %}
                {% set remainingTime = myTimeStop.val - runningTotal.val %}
                {% set currentValue = row['33'].value if row['33'].value <= remainingTime else remainingTime %}
                {% set runningTotal.val = runningTotal.val + currentValue %}
                {
                    "37":{           
                        "value": "{{row['36'].value | int}}"
                    },
                    "39":{           
                        "value": "{{row['8'].value | int}}"
                    },
                    "28":{
                        "value": "{{relatedWorkCode.val | int}}"
                    },
                    "42":{
                        "value": "{{relatedClass.val | int}}"
                    },
                    "655":{           
                        "value": "{{row['13'].value | int}}"
                    },
                    "227":{           
                        "value": "{{currentValue}}"
                    },
                    "34":{           
                        "value": "{{relatedTimeSheet.val | int}}"
                    }
                }
                {% if loop.last == false and runningTotal.val <= myTimeStop.val %},{% endif %}
            {% endfor %}
        ]
        }


        ------------------------------
        Dwight Munson
        ------------------------------