Skip to main content
Question

mixing jinja with to_json pipe

  • May 29, 2023 10:53 AM UTC
  • 0 replies
  • 527 views

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
------------------------------
This topic has been closed for replies.

DwightMunson1
Forum|alt.badge.img+8

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
------------------------------