Hi, Is there a way to iterate through JSON keys wh...
# advancedpdf
s
Hi, Is there a way to iterate through JSON keys when we add custom JSON/Object data source via renderer?
s
Copy code
<#assign myHash = { "name": "mouse", "price": 50 }>
<#list myHash?values as v>
  ${v}
</#list>

<#list myHash?keys as k>
  ${k}
</#list>
s
Unfortunately it doesn’t work when I pass the data via template renderer custom data source. This is the data
{
"date": "1/4/2024",
"day": 1,
"weekDay": "Mon",
"department": "1111 Works council",
"timesheetGroup": "EU Projects",
"hours": "8:00",
"duration": 8,
"wageRate": ".00",
"workPack": "WP1"
}
list does not iterate, output is empty
s
what's your code to pass the customDataSource?
s
Copy code
templateRenderer.addCustomDataSource({
            format: render.DataSource.OBJECT,
            alias: 'timedata',
            data: {
            "date": "1/4/2024",
            "day": 1,
            "weekDay": "Mon",
            "department": "1111 Works council",
            "timesheetGroup": "EU Projects",
            "hours": "8:00",
            "duration": 8,
            "wageRate": ".00",
            "workPack": "WP1"
        }
        });
I’ve also tried to pass stringified JSON and do eval in the PDF but still did not succeed
if I do ${timedata.day} it prints. but if I do list keys/values, nothing is shown
s
What code did you add to your PDF?
s
Copy code
<#list timedata?keys as key>
  ${key}
</#list>
Copy code
<#list timedata as key, value>
  ${key}
</#list>
tried these both
s
weird. I've never had that issue... although I like to embed multiple datapoints like: format: render.DataSource.JSON, alias: "data", data: JSON.stringify({ "timedata" : {...}, "otherstuff" : {...} }) Then in the pdf
<#if data.timedata?has_content><#assign timedata = data.timedata/><#/if>
s
I am doing similar in my original code but was trying to minimize the problem
challenge was to show a dynamic JSON but I handled the logic in Script instead of Freemarker. Thank you for the inputs