# Split JSON string and BASE64 conversion

**URL:** <https://discourse.nodered.org/t/split-json-string-and-base64-conversion/16995>\
**Category:** General\
**Created:** [22 October 2019 06:34 UTC](https://discourse.nodered.org/t/split-json-string-and-base64-conversion/16995 "2019-10-22T06:34:52Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![easyE4](https://avatars.discourse-cdn.com/v4/letter/e/a5b964/32.png) [@easyE4](https://discourse.nodered.org/u/easyE4)\
**Post date:** [22 October 2019 06:34 UTC](https://discourse.nodered.org/t/split-json-string-and-base64-conversion/16995/1 "2019-10-22T06:34:52Z")

</div>

Hello everybody,  
I am a PLC programmer and the world of Node-Red is completely new to me.  
I now want to read data from a small controller, which will be provided to me in a JSON format. The format looks like this:

msg.payload: string [52]  
"{" OPERANDS ": {" MWSINGLE ": [{" INDEX ": 10," V ":" 6QI = "}]}}".

I want to show two values on the dashboard now. The "INDEX" value and the data value "V". The data value "V" is sent in a Base64 format and would have to be converted into a string.

It would be very friendly if someone could help me here in the forum.

Thanks and Greetings  
Ralf

---

<div class="post-metadata">

**Author:** ![kuema](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/kuema/32/6542_2.png) [@kuema](https://discourse.nodered.org/u/kuema)\
**Post date:** [22 October 2019 06:59 UTC](https://discourse.nodered.org/t/split-json-string-and-base64-conversion/16995/2 "2019-10-22T06:59:50Z")

</div>

What have you tried so far?

I can give you some pointers, everything you need is already available in Node-RED. 🙂

First, use a JSON node to parse your payload into a proper object.

Then use the change node to convert `msg.payload.OPERANDS.MWSINGLE[0].V` into a string using a JSONata expression (there a function for that called `$base64decode(string)`). Hope that helps as a starting point.

---

<div class="post-metadata">

**Author:** ![ukmoose](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ukmoose/32/13_2.png) [@ukmoose](https://discourse.nodered.org/u/ukmoose)\
**Post date:** [22 October 2019 07:01 UTC](https://discourse.nodered.org/t/split-json-string-and-base64-conversion/16995/3 "2019-10-22T07:01:41Z")

</div>

also read the docs, especially the page on working with messages which you should find useful

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [22 October 2019 07:37 UTC](https://discourse.nodered.org/t/split-json-string-and-base64-conversion/16995/4 "2019-10-22T07:37:22Z")

</div>

rather than a change node - just use a base64 node that is meant for this purpose :-)...

---

<div class="post-metadata">

**Author:** ![kuema](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/kuema/32/6542_2.png) [@kuema](https://discourse.nodered.org/u/kuema)\
**Post date:** [22 October 2019 07:39 UTC](https://discourse.nodered.org/t/split-json-string-and-base64-conversion/16995/5 "2019-10-22T07:39:53Z")

</div>

I had that in mind at first, too. But it replaces the payload and would require additional steps to save and restore the original message payload before and after.

With the JSONata and change node solution you can convert the property in place.

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [22 October 2019 08:08 UTC](https://discourse.nodered.org/t/split-json-string-and-base64-conversion/16995/6 "2019-10-22T08:08:46Z")

</div>

it can operate on any property within the message... works just fine.

---

<div class="post-metadata">

**Author:** ![kuema](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/kuema/32/6542_2.png) [@kuema](https://discourse.nodered.org/u/kuema)\
**Post date:** [22 October 2019 08:16 UTC](https://discourse.nodered.org/t/split-json-string-and-base64-conversion/16995/7 "2019-10-22T08:16:01Z")

</div>

I have seen that, but I thought that it used to specify the input property to operate on, while the output would always be `msg.payload` (as for most of the other transforming nodes). Thanks for clarifying, works as advertised. 🙂

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [22 October 2019 09:07 UTC](https://discourse.nodered.org/t/split-json-string-and-base64-conversion/16995/8 "2019-10-22T09:07:45Z")

</div>

> [@kuema](#):
>
> ...used to specify the input property to operate on

It does - but it operates on and replaces the property selected - leaving the rest of payload (or whatever) - intact.

---

<div class="post-metadata">

**Author:** ![easyE4](https://avatars.discourse-cdn.com/v4/letter/e/a5b964/32.png) [@easyE4](https://discourse.nodered.org/u/easyE4)\
**Post date:** [22 October 2019 09:17 UTC](https://discourse.nodered.org/t/split-json-string-and-base64-conversion/16995/9 "2019-10-22T09:17:24Z")

</div>

Hello and thank you for your replies.  
I am now a good way ahead. As a result, I now get "6QI =" as the value. What I need now is to convert this value into a number. Can I hope again for your help :-).

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [22 October 2019 09:29 UTC](https://discourse.nodered.org/t/split-json-string-and-base64-conversion/16995/10 "2019-10-22T09:29:06Z")

</div>

so now you need to use either the base64 node ([https://flows.nodered.org/node/node-red-node-base64](https://flows.nodered.org/node/node-red-node-base64)) or the change node and the formula from @kuema above. (The base64 node may need to be installed - but would then be simpler to use in the future.)

That should give you a debug that looks like  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/e/ec524147b8694561df0cfab8ab63416f9885c6b2.png)

so V is now a two byte buffer which presumably you then need to convert into a number in some way... (usually either 256 \* the first, plus the second - or 256 \* the second, plus the first) - but we can't guess that.

---

<div class="post-metadata">

**Author:** ![kuema](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/kuema/32/6542_2.png) [@kuema](https://discourse.nodered.org/u/kuema)\
**Post date:** [22 October 2019 09:38 UTC](https://discourse.nodered.org/t/split-json-string-and-base64-conversion/16995/11 "2019-10-22T09:38:00Z")

</div>

If it's a Buffer, you could also use its nice helper functions for extracting data, like `buf.readInt16LE(0)`. Depending on your endianess (little/big) and expected data type.

More info here: [https://nodejs.org/dist/latest-v10.x/docs/api/buffer.html](https://nodejs.org/dist/latest-v10.x/docs/api/buffer.html)

This would have to be done in a function node where the other operations could be done as well, but I wanted to give you this option for completeness. 🙂

---

<div class="post-metadata">

**Author:** ![easyE4](https://avatars.discourse-cdn.com/v4/letter/e/a5b964/32.png) [@easyE4](https://discourse.nodered.org/u/easyE4)\
**Post date:** [23 October 2019 05:26 UTC](https://discourse.nodered.org/t/split-json-string-and-base64-conversion/16995/12 "2019-10-23T05:26:01Z")

</div>

Hi guys,  
I've read a lot of documentation and tried a few things, but I'm too much of a beginner to find the right solution.  
The integer value in my control is e.g. 610. The two values in the buffer are then [98,2]. Unfortunately, I fail to convert the values in the buffer back to 610.

I do not want to waste your time, but probably only one example with the full function code will help me.

---

<div class="post-metadata">

**Author:** ![kuema](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/kuema/32/6542_2.png) [@kuema](https://discourse.nodered.org/u/kuema)\
**Post date:** [23 October 2019 06:20 UTC](https://discourse.nodered.org/t/split-json-string-and-base64-conversion/16995/13 "2019-10-23T06:20:56Z")

</div>

Assuming your data structure is the same as in your OP and you have converted the base64 property into a Buffer using the `base64` node, the following code (in a `function` node) should replace your Buffer with the decoded value:

```javascript
let buf = msg.payload.OPERANDS.MWSINGLE[0].V;

msg.payload.OPERANDS.MWSINGLE[0].V = buf.readInt16LE(0);

return msg;

```

---

<div class="post-metadata">

**Author:** ![easyE4](https://avatars.discourse-cdn.com/v4/letter/e/a5b964/32.png) [@easyE4](https://discourse.nodered.org/u/easyE4)\
**Post date:** [23 October 2019 07:04 UTC](https://discourse.nodered.org/t/split-json-string-and-base64-conversion/16995/14 "2019-10-23T07:04:47Z")

</div>

Hi kuema,  
I tried your code and get the following error message:

"TypeError: Cannot read property 'MWSINGLE' of undefined".

The function node is behind the base64 node.

---

<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:** [23 October 2019 07:23 UTC](https://discourse.nodered.org/t/split-json-string-and-base64-conversion/16995/15 "2019-10-23T07:23:19Z")

</div>

> [@easyE4](#):
>
> The integer value in my control is e.g. 610. The two values in the buffer are then [98,2]. Unfortunately, I fail to convert the values in the buffer back to 610.

Did you try the suggestion by @kuema ?

> [@kuema](#):
>
> If it's a Buffer, you could also use its nice helper functions for extracting data, like `buf.readInt16LE(0)`  
> More info here: [Buffer | Node.js v10.24.1 Documentation](https://nodejs.org/dist/latest-v10.x/docs/api/buffer.html)

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/c/c03eba6aebc4e002b56b1e8e016539bb8cf0a010.png)

---

<div class="post-metadata">

**Author:** ![kuema](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/kuema/32/6542_2.png) [@kuema](https://discourse.nodered.org/u/kuema)\
**Post date:** [23 October 2019 07:27 UTC](https://discourse.nodered.org/t/split-json-string-and-base64-conversion/16995/16 "2019-10-23T07:27:49Z")

</div>

As I said, it goes by the assumption that the input data was the same as in your first post.

The function expects a structure like this:

```auto
{
"OPERANDS":{
  "MWSINGLE":[
      {
          "INDEX":10,
          "V":[98,2]
      }
  ]}
}

```

Where `[98,2]` is a Buffer.

---

<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:** [23 October 2019 07:29 UTC](https://discourse.nodered.org/t/split-json-string-and-base64-conversion/16995/17 "2019-10-23T07:29:02Z")

</div>

To demonstrate whats happening here

- your data is in bytes [98,2].
- You want integer.

The long hand `byte` to `integer` conversion is as the picture below - HOWEVER - `Buffer` has some nice helper function.

**Long hand version to demonstrate how helpful buffer functions are...**  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/b/bf601c3ef89d877dd6b71594ca366a7f3d53a5de.png)

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [23 October 2019 07:43 UTC](https://discourse.nodered.org/t/split-json-string-and-base64-conversion/16995/18 "2019-10-23T07:43:06Z")

</div>

To all those helping - please note that in the OP there were spaces in some of the variable names like " V " for example... which is awful I know but will catch you out in your example code above...

---

<div class="post-metadata">

**Author:** ![kuema](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/kuema/32/6542_2.png) [@kuema](https://discourse.nodered.org/u/kuema)\
**Post date:** [23 October 2019 07:48 UTC](https://discourse.nodered.org/t/split-json-string-and-base64-conversion/16995/19 "2019-10-23T07:48:31Z")

</div>

I noticed that and removed them. Thought they were introduced by copy-paste or something.  
If there are indeed spaces in there, my code will not work, of course. 😑

---

<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:** [23 October 2019 08:44 UTC](https://discourse.nodered.org/t/split-json-string-and-base64-conversion/16995/20 "2019-10-23T08:44:02Z")

</div>

To get over the spaces problem if you cannot remove them before they get to node red then use syntax like  
`msg.payload[" OPERANDS "][" MWSINGLE "][0][" V "]`  
It would, of course, be much neater if they could be removed at source.

[Next page](https://discourse.nodered.org/t/split-json-string-and-base64-conversion/16995.md?page=2)
