'How to parse json nested attributes using Jinja
I am relatively new to using Jinja and have questions. I was wondering if there is a different solution to parsing nested JSON attributes using Jinja? The below solution seems fine for a small payload. But, I am wondering if there is a better way? This solution doesn't seem like it would be effective if I was using a larger payload.
Sample Payload:
{
"campaign_id":"123456789",
"email":"[email protected]",
"template_attributes":{
"notificationDate" : "January 01, 2022",
"orderNumber" : "1234567",
"orderDateTime" : "01/01/2022 11:11 PM",
"orderDate" : "January 01, 2022",
"billingInfo" : {
"firstName" : "Tester",
"lastName" : "Mctesterson",
"addressLine1" : "1234 TEST ST",
"city" : "TESTVILLE",
"state" : "TN",
"postalCode" : "12345",
"phoneNumber" : "1234567890",
"country" : "US",
"customerEmailAddress" : "[email protected]"
}
}
}
Jinja used:
{%if billingInfo is defined%}
<p style="font-family: Arial, sans-serif; font-size: 16px; color: #333333;">
{{billingInfo.firstName}} {{billingInfo.lastName}}<br>
{{billingInfo.addressLine1}}<br>
{%if billingInfo.addressLine2 %} {{billingInfo.addressLine2}}<br> {%endif%}
{{billingInfo.city}}, {{billingInfo.state}} {{billingInfo.postalCode}}<br>
</p>
{%endif%}
Output:
Tester Mctesterson
1234 TEST ST
TESTVILLE, TN 12345
Proposed Larger Payload:
{
"campaign_id":"123456789",
"email":"[email protected]",
"template_attributes":{
"notificationDate" : "January 01, 2022",
"orderNumber" : "1234567",
"orderDateTime" : "01/01/2022 11:11 PM",
"orderDate" : "January 01, 2022",
"billingInfo" : {
"firstName" : "Tester",
"lastName" : "Mctesterson",
"addressLine1" : "1234 TEST ST",
"city" : "TESTVILLE",
"state" : "TN",
"postalCode" : "12345",
"phoneNumber" : "1234567890",
"country" : "US",
"customerEmailAddress" : "[email protected]"
},
"PaymentInfoList" : {
"PaymentInfo" : {
"paymentMethod" : "Credit Card [Test]",
"cardBrand" : "Test",
"accountNumberLast4" : "1234",
"paymentAmount" : "12.34"
}
}
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
