How to pass Json Data from "function node" to "template node"

Hi, I am Vinay

I am not understanding How to pass Json data from "function node" to "template node".

My sample Json data is below


I am placing Json data in function node and I should fetch that Json data into Template Node

Attach a debug node after the function.

Then read this page in the docs that will explain how to use the debug panel to find the right path to any data item.

Pay particular attention to the part about the buttons that appear under your mouse pointer when you over hover a debug message property in the sidebar.

BX00Cy7yHi

https://nodered.org/docs/user-guide/messages

Then once you have copied the path, paste that into the {{mustache}} in the template

Also, read the built in help for the template node.

Hi
I have copied the path and pasted in template using below command

image

image

I have used both but it is not getting

In console it is showing unexpected identifier

It would.

You asked about the template node. The function node doesn't use mustache.

Also, in a function node, you need to specify the msg e.g...

node.log(msg.payload.Students)

If you want to list the array items the template mustache syntax woulbe be as below
example flow

[{"id":"1b38a685.e629e9","type":"inject","z":"c74669a0.6a34f8","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"student\":[{\"name\":\"tom\",\"class\":10},{\"name\":\"john\",\"class\":10}],\"employee\":[{\"name\":\"tim\",\"class\":10},{\"name\":\"jane\",\"class\":10}]}","payloadType":"json","x":190,"y":2360,"wires":[["bff1f319.af36a8"]]},{"id":"bff1f319.af36a8","type":"template","z":"c74669a0.6a34f8","name":"","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"Students\n{{#payload.student}}\n Name {{name}}  Class {{class}}\n{{/payload.student}}\n\nEmployees\n{{#payload.employee}}\n Name {{name}}  Class {{class}}\n{{/payload.employee}}","output":"str","x":350,"y":2360,"wires":[["4f6e9447.bba25c"]]},{"id":"4f6e9447.bba25c","type":"debug","z":"c74669a0.6a34f8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","statusVal":"payload","statusType":"auto","x":570,"y":2360,"wires":[]}]

template

Students
{{#payload.student}}
 Name {{name}}  Class {{class}}
{{/payload.student}}

Employees
{{#payload.employee}}
 Name {{name}}  Class {{class}}
{{/payload.employee}}

object used, as you failed to supply one that can be copied

{
    "student": [
        {
            "name": "tom",
            "class": 10
        },
        {
            "name": "john",
            "class": 10
        }
    ],
    "employee": [
        {
            "name": "tim",
            "class": 10
        },
        {
            "name": "jane",
            "class": 10
        }
    ]
}

[edit After looking at your other posts, i doubt what you want to do (not that I fully understan) is possible as the console.log is server side and the code in the template I suspect will be client side. But as you do not explain what exactly you want to do one can only guess.
here is another example but I doubt it will do what you want. If you want more help i think you will need to explain more clearly what you are attempting to do.

[{"id":"1b38a685.e629e9","type":"inject","z":"c74669a0.6a34f8","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"student\":[{\"name\":\"tom\",\"class\":10},{\"name\":\"john\",\"class\":10}],\"employee\":[{\"name\":\"tim\",\"class\":10},{\"name\":\"jane\",\"class\":10}]}","payloadType":"json","x":110,"y":2360,"wires":[["b4039d87.784778"]]},{"id":"b4039d87.784778","type":"change","z":"c74669a0.6a34f8","name":"","rules":[{"t":"set","p":"payload.student","pt":"msg","to":"$string(payload.student)","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":280,"y":2320,"wires":[["bff1f319.af36a8"]]},{"id":"bff1f319.af36a8","type":"template","z":"c74669a0.6a34f8","name":"","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"<script>\nconsole.log({{{payload.student}}})\n</script>","output":"str","x":370,"y":2360,"wires":[["4f6e9447.bba25c"]]},{"id":"4f6e9447.bba25c","type":"debug","z":"c74669a0.6a34f8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":530,"y":2360,"wires":[]}]

]

How to access same Json data into script tag of mustache

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.