Convert string to JSON object using freemarker template

55 Views Asked by At
<#assign loanInformation = '{"8":{"loanId":"8","employeeId":"7","loanType":"1","totalAmount":"100000","installmentAmount":"6666.67","remainingBalance":"93333.33","loanTitle":"ELR - EMP739 Umar Naeem (5/1/2024) (House Loan)"},"9":{"loanId":"9","employeeId":"7","loanType":"2","totalAmount":"100000","installmentAmount":"10000","remainingBalance":"90000.0","loanTitle":"ELR-9 (Car Loan)"}}'>

<#assign loanInfoJason = loanInformation?eval_json>
<#list loanInfoJason as loan>
  <tr class="bb">
    <td align="center" class="br">${loan.loanTitle}</td>
    <td align="center" class="br">${loan.installmentAmount}</td>
    <td align="center">${loan.remainingBalance}</td>
  </tr>
</#list>

This works fine in freemarker templating but if

<#assign loanInformation = record.custrecord_dsc_psf_loan_ded_info_json>

Getting Error

Failed to "?eval_json" string with this error: ---begin-message--- Invalid JSON keyword: "Lorem". Should be one of: true, false, null. If it meant to be a string then it must be quoted. Error location: line 1, column 1: Lorem ipsum dolor sit amet consectetuer ac orci... ^ ---end-message--- The failing expression: ==> loanInformation?eval_json [in template "template" at line 290, column 25] ---- FTL stack trace ("~" means nesting-related): - Failed at: #assign loanInfo = loanInformation... [in template "template" at line 290, column 5]

eval_json should able to return valid JSON when use variable.

1

There are 1 best solutions below

0
d.k On

eval_json should able to return valid JSON when use variable.

No, you are wrong on the conceptual level. eval_json returns you a data structure (usually depends on the environment/programming language one is using), but that data is derived from a valid JSON string. In your example, when you use your hard coded string on line 1 <#assign loanInformation = '{"8":{"loan … it's able to provide a value, which as I said is not JSON But when you take a value from the record's field custrecord_dsc_psf_loan_ded_info_json, which contains a lorem ipsum text, of course there is no way to reasonably determine the data structure from it, so the JSON parser throws.

Most likely you have a record with a field custrecord_dsc_psf_loan_ded_info_json. And the field contains just a 'lorem ipsum' text, and you are trying to process it as JSON. Of course this wouldn't work.

Put a JSON value into the field. Like the one you have hardcoded in your template:

{
    "8": {
        "loanId": "8",
        "employeeId": "7",
        "loanType": "1",
        "totalAmount": "100000",
        "installmentAmount": "6666.67",
        "remainingBalance": "93333.33",
        "loanTitle": "ELR - EMP739 Umar Naeem (5/1/2024) (House Loan)"
    },
    "9": {
        "loanId": "9",
        "employeeId": "7",
        "loanType": "2",
        "totalAmount": "100000",
        "installmentAmount": "10000",
        "remainingBalance": "90000.0",
        "loanTitle": "ELR-9 (Car Loan)"
    }
}

instead of lorem ipsum dolor sit amet