# How to build a JSON in a defined format

**URL:** <https://discourse.nodered.org/t/how-to-build-a-json-in-a-defined-format/84093>\
**Category:** General\
**Created:** [28 December 2023 17:24 UTC](https://discourse.nodered.org/t/how-to-build-a-json-in-a-defined-format/84093 "2023-12-28T17:24:08Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![knaxman](https://avatars.discourse-cdn.com/v4/letter/k/d07c76/32.png) [@knaxman](https://discourse.nodered.org/u/knaxman)\
**Post date:** [28 December 2023 17:24 UTC](https://discourse.nodered.org/t/how-to-build-a-json-in-a-defined-format/84093/1 "2023-12-28T17:24:08Z")

</div>

Hi,

I stuck in a pretty basic propblem. Could someone give me a hint please.

My challenage:

I need to compile a MQTT Message with a fixed format. The Value I want to send I have, but I do not understand how to build exactly this JSON format to send it afterwards as MQTT out.

Target Format:  
{  
"pv": {  
"power": 0.0,  
"L1": {  
"power": 0.0  
}  
}  
}

0.0 is the actual measurement value I want to get into the message.

How can I do this?

I tried the template function but receive always an error.

that's my node.  
`[{"id":"b2aab1de18a3670f","type":"template","z":"06d63c099a670eab","name":"Build JSON","field":"payload","fieldType":"msg","format":"json","syntax":"plain","template":"{\n \"pv\": {\n \"power\": payload,\n \"L1\": {\n \"power\": payload\n }\n }\n}","output":"json","x":1890,"y":460,"wires":[["03267eaa74855d0d"]]},{"id":"f35c88ebfcbe16d7","type":"inject","z":"06d63c099a670eab","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"144","payloadType":"num","x":1650,"y":460,"wires":[["b2aab1de18a3670f"]]},{"id":"03267eaa74855d0d","type":"debug","z":"06d63c099a670eab","name":"debug 6","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":2100,"y":460,"wires":[]}] type or paste code here `

I know this might be a very basic question; I would be happy if someone can give me a hint.

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [28 December 2023 17:34 UTC](https://discourse.nodered.org/t/how-to-build-a-json-in-a-defined-format/84093/2 "2023-12-28T17:34:07Z")

</div>

> [@knaxman](#):
>
> template

Did you read the built in help on the sidebar (to the right)?

I'm certain it shows you how to add variable using {{mustache}} syntax

---

<div class="post-metadata">

**Author:** ![knaxman](https://avatars.discourse-cdn.com/v4/letter/k/d07c76/32.png) [@knaxman](https://discourse.nodered.org/u/knaxman)\
**Post date:** [28 December 2023 17:36 UTC](https://discourse.nodered.org/t/how-to-build-a-json-in-a-defined-format/84093/3 "2023-12-28T17:36:50Z")

</div>

Thanks, "{{ }}" was the hint I needed.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [26 February 2024 17:37 UTC](https://discourse.nodered.org/t/how-to-build-a-json-in-a-defined-format/84093/4 "2024-02-26T17:37:38Z")

</div>

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