# Message formatting from JSON to Object/Array

**URL:** <https://discourse.nodered.org/t/message-formatting-from-json-to-object-array/45657>\
**Category:** General\
**Created:** [14 May 2021 14:46 UTC](https://discourse.nodered.org/t/message-formatting-from-json-to-object-array/45657 "2021-05-14T14:46:51Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![DuncanCT](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/duncanct/32/41639_2.png) [@DuncanCT](https://discourse.nodered.org/u/DuncanCT)\
**Post date:** [14 May 2021 14:46 UTC](https://discourse.nodered.org/t/message-formatting-from-json-to-object-array/45657/1 "2021-05-14T14:46:51Z")

</div>

Warning this isn't going to be pretty, I pretty sure non of my terminology will be correct. Don't think it needs saying but I don't really know what I'm doing.

I'm trying to convert a message from JSON to Object/Array? notation. Thought I got close but in the example linked I'm getting and extra "object/array child".

What I need:  
0: \> "measurement" : "testDevice"

What I'm getting:  
0: \> "measurement" : \> "payload" : "testDevice"

Think my lack of terminaloly if failing me here, as Google might be more help if I new how to search the problem. ☹

Function code:

```auto
var msg1 = {payload:msg.payload.device};
var msg2 = {payload:msg.payload.data};

msg.payload = [
    {
        measurement: msg1,
        fields: {
            data: msg2
        }
    }
];
return msg;

```

Node Example:

```auto
[{"id":"f3abc4cf.bf56a8","type":"inject","z":"ec5aafa2.fcafb","name":"JSON String","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"device\":\"testDevice\",\"data\":\"testData\"}","payloadType":"json","x":790,"y":420,"wires":[["d9595840.361e38"]]},{"id":"d9595840.361e38","type":"function","z":"ec5aafa2.fcafb","name":"","func":"var msg1 = {payload:msg.payload.device};\nvar msg2 = {payload:msg.payload.data};\n\nmsg.payload = [\n {\n measurement: msg1,\n fields: {\n data: msg2\n }\n }\n];\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":980,"y":420,"wires":[["23112814.a43278"]]},{"id":"23112814.a43278","type":"debug","z":"ec5aafa2.fcafb","name":"Incorrect","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1160,"y":420,"wires":[]},{"id":"281bada5.7f2602","type":"function","z":"ec5aafa2.fcafb","name":"","func":"msg.payload = [\n {\n measurement: \"testDevice\",\n fields: {\n data: \"testData\",\n },\n }\n];\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":980,"y":380,"wires":[["1289e8ef.e81067"]]},{"id":"4def3a8e.7d8624","type":"inject","z":"ec5aafa2.fcafb","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":780,"y":380,"wires":[["281bada5.7f2602"]]},{"id":"1289e8ef.e81067","type":"debug","z":"ec5aafa2.fcafb","name":"Correct","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1160,"y":380,"wires":[]}]

```

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [14 May 2021 15:11 UTC](https://discourse.nodered.org/t/message-formatting-from-json-to-object-array/45657/2 "2021-05-14T15:11:38Z")

</div>

I am afraid your notation is a bit awry as you suggest. The output of the lower inject node is not json, it is a javascript object. The string you have entered into the inject node config is json, but you have told it to convert that to a javascript object.

You are nearly there, you are overthinking the problem. All you need it something like

```auto
msg.payload = [
    {
        measurement: msg.payload.device,
        fields: {
            data: msg.payload.data
        }
    }
];
return msg;

```

or if you want to pick up the values to a local variable first then

```auto
let measurement = msg.payload.device
let data = msg.payload.data

msg.payload = [
    {
        measurement: measurement,
        fields: {
            data: data
        }
    }
];
return msg;

```

Have a read of this page from the docs, it should help you to understand messages. [Working with messages : Node-RED](https://nodered.org/docs/user-guide/messages)

---

<div class="post-metadata">

**Author:** ![shrickus](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/shrickus/32/517_2.png) [@shrickus](https://discourse.nodered.org/u/shrickus)\
**Post date:** [14 May 2021 16:29 UTC](https://discourse.nodered.org/t/message-formatting-from-json-to-object-array/45657/3 "2021-05-14T16:29:05Z")

</div>

Just to provide another way to accomplish the same thing: I like using a JSONata expression inside a `change` node for cases like this. For your sample data, the `J:` expression would be:

```auto
[
    {
        measurement: payload.device,
        fields: {
            data: payload.data
        }
    }
]

```

JSONata uses a less familiar "lambda" expression syntax, which looks similar to JS -- and so most people prefer to avoid it and use plain javascript. But it provides a rather powerful syntax to transform JS object structures... similar in design to the way XSLT uses XPath to transform XML structures.

FWIW, I think it's a nice tool to have at hand -- and while it's easy to learn, it does take a while to master. If you want to learn more, you can try it out at [try.jsonata.org](https://try.jsonata.org)

---

<div class="post-metadata">

**Author:** ![DuncanCT](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/duncanct/32/41639_2.png) [@DuncanCT](https://discourse.nodered.org/u/DuncanCT)\
**Post date:** [14 May 2021 17:33 UTC](https://discourse.nodered.org/t/message-formatting-from-json-to-object-array/45657/4 "2021-05-14T17:33:43Z")

</div>

Thank you guys, just the input I needed.

@Colin, sorry I wasn't very articulate on my requirements. I wanted the JSON inject to output at the debug as a javascript object, to match the example provided on top. I fear I'm just typing in circles now. 🤣 Anyway, thanks again!

@shrickus, thanks, you might have lost me a bit but I'll do some homework.

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [14 May 2021 17:43 UTC](https://discourse.nodered.org/t/message-formatting-from-json-to-object-array/45657/5 "2021-05-14T17:43:17Z")

</div>

I think your code is confusing people. If you require just an array containing a object with one key value pair, it would be more like below.

```auto
msg.payload = [
    {
    measurement: msg.payload.data
    }
];

```

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [14 May 2021 17:47 UTC](https://discourse.nodered.org/t/message-formatting-from-json-to-object-array/45657/6 "2021-05-14T17:47:46Z")

</div>

> [@shrickus](#):
>
> ```auto
> [
> {
> measurement: payload.device,
> fields: {
> data: payload.data
> }
> }
> ]
> 
> ```

Would that not be "Invalid JSONata expression, Key must evaluate to a string" as your keys would need to be in quotes.

---

<div class="post-metadata">

**Author:** ![DuncanCT](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/duncanct/32/41639_2.png) [@DuncanCT](https://discourse.nodered.org/u/DuncanCT)\
**Post date:** [14 May 2021 17:51 UTC](https://discourse.nodered.org/t/message-formatting-from-json-to-object-array/45657/7 "2021-05-14T17:51:53Z")

</div>

Maybe back round would have been a better place to start.

I am taking a callback and sending it to InfluxDB. The callback arrives as a JSON (string/object - please correct me to which one) as in my example of the JSON inject node. As with the example from the InfluxDB batch node ([node-red-contrib-influxdb (node) - Node-RED](https://flows.nodered.org/node/node-red-contrib-influxdb)), it needs to be a javascript object.

My understanding of all this is very limited so apologies for the confusion but thank you for the help.

---

<div class="post-metadata">

**Author:** ![shrickus](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/shrickus/32/517_2.png) [@shrickus](https://discourse.nodered.org/u/shrickus)\
**Post date:** [14 May 2021 17:54 UTC](https://discourse.nodered.org/t/message-formatting-from-json-to-object-array/45657/8 "2021-05-14T17:54:35Z")

</div>

Of course, you are right... too big a hurry.

And now I see he wants a simpler output format anyway.

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [14 May 2021 20:21 UTC](https://discourse.nodered.org/t/message-formatting-from-json-to-object-array/45657/9 "2021-05-14T20:21:26Z")

</div>

> [@DuncanCT](#):
>
> The callback arrives as a JSON (string/object - please correct me to which one)

Feed it into a debug node and if it says it is a string it is a string. If it is in JSON format then it is a JSON string, also known as a JSON object. If the debug node shows it as an object then it is a javascript object.

Whichever it is, show us what it looks like so we can understand what you are starting with.

---

<div class="post-metadata">

**Author:** ![L1Blom](https://avatars.discourse-cdn.com/v4/letter/l/74df32/32.png) [@L1Blom](https://discourse.nodered.org/u/L1Blom)\
**Post date:** [14 May 2021 20:48 UTC](https://discourse.nodered.org/t/message-formatting-from-json-to-object-array/45657/10 "2021-05-14T20:48:04Z")

</div>

Looks like you are looking for JSON.parse(string) with string is a json-structure.  
I have no access to my node-red instance right now to test your code.

---

<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:** [13 July 2021 20:48 UTC](https://discourse.nodered.org/t/message-formatting-from-json-to-object-array/45657/11 "2021-07-13T20:48:19Z")

</div>

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