# OPCUA data parsing issue

**URL:** <https://discourse.nodered.org/t/opcua-data-parsing-issue/54964>\
**Category:** General\
**Created:** [9 December 2021 21:50 UTC](https://discourse.nodered.org/t/opcua-data-parsing-issue/54964 "2021-12-09T21:50:15Z")\
**Posts on this page:** 7\
**Page:** 2

<div class="post-metadata">

**Author:** ![dtid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dtid/32/52368_2.png) [@dtid](https://discourse.nodered.org/u/dtid)\
**Post date:** [21 December 2021 17:43 UTC](https://discourse.nodered.org/t/opcua-data-parsing-issue/54964/21 "2021-12-21T17:43:09Z")

</div>

Ok I set up two streams. One with just getting the raw output and then the other with the change node  
First I get the raw output and copy:

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/9/4/944e6c907b614fa24c8cc2aa0d3aa8d004deb7db.png)

Then Paste it in the test field of the change node:

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/b/a/ba29ea1c72c573c907dcf1516e4c1b1f6203c117.png)

Is there a command I can send to the OPC server to ask it what type of character encoding its using?  
How would I pass an environment variable to the opc server to test with vt100 etc?

---

<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:** [21 December 2021 20:30 UTC](https://discourse.nodered.org/t/opcua-data-parsing-issue/54964/22 "2021-12-21T20:30:00Z")

</div>

Javascript strings handle all UTF-8 characters, so the "squares" will not affect the string handling -- and the regex will be ignoring those characters anyway.

The error message is because you need a valid `msg` object in the example data, as @struland mentioned. This is why I suggested that you replace (only) the test string "hello world" with your data, wrapped in double quotes, and with `\n` replacing any newline characters. This is required, so the test data is valid JSON, and matches what would have been in the original debug msg.

You can either use the debug sidebar "Copy value" button to grab the entire msg (if you are showing the complete msg), OR put your data in an object wrapper, like so:

```
{
    "payload": "... your data goes here \n all on one line \n with \n in place of newlines..."
}

```

Now that I look at your data in the debug messages, I think you will also have issues with the regex using that `e\[3` syntax I wrote earlier -- it works with your sample data because that little square char is an escape and it copied as the letter 'e'. You can match any one char before the left square brackets by changing that section of the regex to `.\[3`

_Edit:_  
I found that to match the ANSI escape char (hex 1B) the correct regex syntax would be:  
`.$match(/^\x1B33m (\w+) .*: (\S+)/)` WITHOUT the square bracket  
(since it is part of the escape sequence) although I have not tried it. Good luck!

---

<div class="post-metadata">

**Author:** ![dtid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dtid/32/52368_2.png) [@dtid](https://discourse.nodered.org/u/dtid)\
**Post date:** [22 December 2021 13:49 UTC](https://discourse.nodered.org/t/opcua-data-parsing-issue/54964/23 "2021-12-22T13:49:47Z")

</div>

> [@shrickus](#):
>
> .$match(/^\x1B33m (\w+) .\*: (\S+)/)

Thank you for that! I am sorry I borked the replace only the hello world section.  
I've tried with the correct regex syntax but have run into another issue though. I cant seem to get any results, I end up with this unexpected token error:

Invalid example JSON message:  
SyntaxError: Unexpected token e in JSON at position 19

---

<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:** [22 December 2021 14:17 UTC](https://discourse.nodered.org/t/opcua-data-parsing-issue/54964/24 "2021-12-22T14:17:25Z")

</div>

> [@dtid](#):
>
> SyntaxError: Unexpected token e in JSON at position 19

I too am having trouble with the JSONata example tester, while trying to use data with non-printable `esc` characters in it... this was not problem when I was using the copied/pasted ascii string.

If you don't mind attaching your data as a file, I can re-write the regex processing in plain JS for use in a `function` node. That would bypass any funny string handling between node-red and the jsonata library.

---

<div class="post-metadata">

**Author:** ![dtid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dtid/32/52368_2.png) [@dtid](https://discourse.nodered.org/u/dtid)\
**Post date:** [22 December 2021 14:34 UTC](https://discourse.nodered.org/t/opcua-data-parsing-issue/54964/25 "2021-12-22T14:34:04Z")

</div>

Attached is the copied output as well as the flow.  
Should I save the text file in a different format or does this work?  
[flows.json](https://discourse.nodered.org/uploads/short-url/c3pQsfyt1wenmG2CSF57EMA9hjc.json) (19.4 KB)  
[MachineInfo.txt](https://discourse.nodered.org/uploads/short-url/1Y8MsUmlCSJjGbHGA6v0geXriO2.txt) (831 Bytes)

---

<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:** [23 December 2021 00:50 UTC](https://discourse.nodered.org/t/opcua-data-parsing-issue/54964/26 "2021-12-23T00:50:46Z")

</div>

Using a `file read` node, I was able to get your data with `esc` chars into the payload. The good news is that a regex CAN work in a JSONata expression -- but the test tab requires a valid JSON string, and so cannot work with your data directly.

Here is the exported `change` node I used:

```auto
[{"id":"38562d4f8250e38d","type":"change","z":"7875ce28fe8aee2d","name":"J:$match","rules":[{"t":"set","p":"props","pt":"msg","to":"payload\t .$split('\\n')\t .$match(/\\x1B\\[33m (\\w+) .*: (.*)/m)\t .[groups] {\t $[0]: $[1].$replace(/\\x1B\\[\\d\\dm/, '')\t }","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":580,"y":560,"wires":[["521b07ccb0e6c506"]]}]

```

and here is the equivalent `function` code:

```auto
let lines = msg.payload.split('\n');
msg.props = {}; // empty object
for (const line of lines) {
    let parts = line.match(/\x1B\[33m (\w+) .*: (.*)/);
    if (parts) {
        msg.props[parts[1]] = parts[2].replace(/\x1B\[\d\dm/g, '');
    }
}
return msg;

```

---

<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:** [21 February 2022 00:50 UTC](https://discourse.nodered.org/t/opcua-data-parsing-issue/54964/27 "2022-02-21T00:50:49Z")

</div>

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

[Previous page](https://discourse.nodered.org/t/opcua-data-parsing-issue/54964.md?page=1)
