Search code examples
jsonjsrender

Accessing data from the root of a json object JSRender


I am currently creating a report using JSReport. I am now stuck in a scenario wherein I need to access data from inside the for loop. Here is a sample of the json

{
    "report_date": "",
    "account": "",
    "account_number": "",
    "data_retention": "",
    "data_purged": "",
    "sms_camp_del": "",
    "numbers_purged": "",
    "message_del": "",
    "reports": [{
        "count": 1,
        "campaign_name": "",
        "date": "",
        "start_time": "",
        "messages": "",
        "numbers": ""
    ...

And I already have this code

{{for reports}}
    ...
{{/for}}

and from the inside of the for loop I want to access the report_date and the account by using {{:report_date}} and {{:account}}, but the problem is that I cannot seem to output their respective values. How could I possibly access everything outside of the reports? Thank you in advance


Solution

  • Yes, you can use contextual parameters:

    {{for reports ~rd=report_date ~ac=account}}...{{/for}}
    

    and access using {{:~rd}} and {{:~ac}}

    Another way is to use {{:~root.report_date}} and {{:~root.account}}.

    See Accessing parent data and Accessing root data: the built-in '~root' contextual parameter.