Node-Red Encountering Problems with Parsing Data

I have successfully set up Node-RED and it is receiving data from multiple PLC systems.

Here is an example of the payload:

{"d":779,"dt":3,"ts":"2018-04-05T19:54:12.930758Z","q":18}

The fields breakdown as follows:

d=779  dt=3  ts=2018-04-05T19:54:12.930758Z  q=18

I've been trying to parse the data, particularly focusing on extracting the "d" field. I've experimented with functions using splice and split methods.

Is there a simple solution to this problem? Could the issue lie in the choice of the node process?

This code snippet was my initial attempt at extracting the first field, but it's not working correctly as it doesn't account for padding and needs to start at the colon and end at the comma.

var str = msg.payload;
var th = str.slice(5,-1); 
th = th.split(",");
msg.payload[0] = parseFloat(th[0]);
return [ msg.payload[0]]; 

The MQTT Buffer looks like this:

[123,34,100,34,58,51,48,54,8,44,34,100,116,34,58,2,44,34,ts,34,58,4,8,8-" "8:" .8,,,8].decode('hex')]

The zero at the end seems to be causing problems:

I'm trying to remove it using this function:

var data = msg.payload;
console.log("payload data: length = " + data.length);
data = data.trim();
console.log("trimmed data: length = " + data.length);

msg.payload = JSON.parse(data);
return msg;

However, now I'm encountering a TypeError: data.trim is not a function

The flow diagram is displayed below:

View Image Here

Thank you, Kevin

Answer №1

Typically, nodes will output a JSON object by default, so you can access the values directly without needing to parse it as a string. For example:

var d = msg.payload.d;

If the nodes are outputting String representations of JSON, you should use the built-in JSON node to convert the string in msg.payload to a JSON object before accessing the values.

https://i.sstatic.net/kXLWR.png

Additionally, ensure that a function node returns a complete JSON object, not just a string (or an array of strings).

So if you only want to pass the value of 'd', it should be handled like this:

var d = msg.payload.d;
msg.payload = d;
return d;

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

ms-card malfunctioning due to data issues

I'm facing difficulties in transferring the data to the template. Although I can access the data in HTML using vm.maquinas and maquina, I am unable to pass it to the TEMPLATE through ng-model. Information about ms-cards was not abundant. Module ang ...

The element should remain vibrant, yet it seems to be fading away

My situation is exactly as the title suggests - there's an element that should not be fading, but it is. The specific element in question is text overlaid on an image that fades when you hover your mouse over it. HTML: <div id="image-wrapper"> ...

Positioning oversized images in a React Native application

Looking to showcase two images side by side using React Native, where I can customize the screen percentage each image takes up. The combined size of the images will exceed the horizontal screen space available, so I want them to maintain their original di ...

Steps to clear all HTML input fields after closing an angular modal

Inputting a username and password into the text field without saving, then closing the popup modal seems to be a common occurrence. If I happen to click outside of the modal, it conveniently closes on its own. However, when reopening the popup, the previ ...

An error occurred: Unable to execute e(...).parents(...).andSelf on Kendo UI Grid

Having trouble with the Kendo UI Grid - every time I click on the add new button, a script error pops up. Check out the issue https://i.sstatic.net/c7Dja.png This is the code snippet in question: A string of code here... Whenever I try to add or edit an ...

Unexpected Issue with JavaScript Ajax (Using jQuery.post): The Promise State Turns to "Rejected"

Recently, I've been encountering some issues while trying to debug my jQuery.post() call. The responses I'm getting are quite puzzling and I'm at a loss on how to proceed next. If anyone has any suggestions or insights, I would greatly appre ...

Resize the div based on the width and height of the window

My goal is to create a system or website that can be fully resizable using CSS and the VW (viewport width) unit. Within this system, I have integrated a GoogleChart that functions with pixels. Now, I am looking for a way to scale the chart using Javascript ...

The jQuery validation applied to the data submission per post is not functioning as expected as the $_POST variable is appearing empty

I'm facing an issue with Jquery, AJAX, and field validation. Although the validation and redirect are working fine, I'm having trouble retrieving values from fields in the $_POST array. Below is the start tag of the form: <form class="smart ...

Generate several sliders using JQuery effortlessly

As I was attempting to create multiple sliders with JQuery in a more automated fashion using iteration, several questions arose (you can see a functional example below). Why does the first block of JQuery code work while the second block of JavaScript cod ...

Leveraging jQuery to manipulate an SVG file

jQuery is designed to work within HTML pages that contain JavaScript code. While SVG and HTML both use the same DOM Level 2, SVG is XML-based and employs ECMAScript. What potential issues could arise from utilizing jQuery with SVG? Is it more advisable t ...

What is the best way to find an onmouseover element with Selenium in Python?

I've been attempting to scrape a website and encountered an element that reveals information in a bubble when the mouse hovers over it. I am using Selenium for web scraping, but I am unsure how to locate this specific element. After examining the pag ...

Validation of phone numbers based on country codes

How can I validate phone numbers based on the selected country in Angular? Are there any Angular packages specifically for this task? I've attempted to use regex, but it only works for certain countries. I need a solution that can validate mobile an ...

Ways to categorize JSON information based on a specified range

Hello, I'm new to the world of programming and would greatly appreciate any guidance and assistance. ...

What is the best way to convert a JSON string from a specific array into an array object?

JSON object: { "data": { "myStatus": "[1 2 21 0 50 0 0 0],[2 1 3 1 50 0 0 0]" }, "success": true } Upon converting to the JSON object, it appears that the myStatus is being treated as a string. The goal is to parse it as an Array inst ...

The filtering function using jQuery and ajax

Hello, I'm currently working on a website that compares the prices of domain names. I could use some assistance with a specific function that I'm trying to implement. At the moment, there is a section on the website where users can click on dif ...

Updating row color according to values obtained from the map function in ReactJs

I have been experimenting with various methods to change the color of table rows based on specific values within a map function. Despite trying solutions like the UseRef hook and browsing through stack overflow, I have yet to achieve success. {dat ...

Angular JS Directive is a powerful feature that allows developers

There are three HTML screens available. Screen1.html: <button id="screenbtn"></button> Screen2.html: <div id="sctmpl"> <label>Label for screen two</label> </div> Screen3.html: <div id="sctmpl1"> <l ...

Looking for a way to trigger a function on JSTree's refresh event

I have searched through many posts here but cannot find what I am looking for. The version of JSTree that I am using is 3.3.3. My goal is to select a node after a create_node event triggers an AJAX request. Subsequently, JSTree fires the rename_node eve ...

Difficulty with Bootstrap 4 mobile navbar dropdown feature

<div class="baslik baslik1 baslik2 "> <nav class="navbar bg-light navbar-light navbar-expand-sm sticky-top "> <a href="./index.html" class="navbar-brand"><img src="img/512x512logo.png" ...

Angular - Error: Cannot read property 'publishLast' of undefined

My goal is to prevent multiple requests from being created when using the async pipe. I am facing an issue with a request to fetch a user from the API: getUser() { this._user = this.http.get<User>(environment.baseAPIUrl + 'user') ...