Help parsing nested JSON data

Hi,

I intend to monitor data from a PLC in the form of a bar graph. I am able to get the JSON data, but I'm new to node red and I'm having trouble extracting data from JSON in msg.payload.

My JSON looks something like this:

{
   "timestamp":1566387382870,
   "values":[
      {
         "instanceId":0,
         "resourceId":5700,
         "datatype":"Float",
         "value":500,
         "objectId":3300
      }
   ]
}

At the moment, I am trying to display only 'value' of 500.

I've been looking though these forums and the documentation and so far, I have been able to show 'timestamp', and all the content of 'values', but I can't figure out a way to display only 'value'.

This is what I have so far in my function:

var obj = JSON.parse(msg.payload); // Stores JSON text as an object.
var valuee = JSON.stringify(obj.values);
msg.payload = valuee;
return msg;

Sorry if the answer is simple, I'm not very familiar with node-red yet, so if somebody could help me out, I would be grateful. Thanks!

You don't say where you are getting the data from.
If it is a JSON string then you can convert it to a javascript object ( which is what you manipulate in Node-RED using the JSON node (or the first line of your function)

But often the "input" nodes are sensible enough to output a javascript object for you.
Add a debug and you should see it will say if it is a string or an object.

This page in the docs https://nodered.org/docs/user-guide/messages explains how to then use the debug panel to find the correct path to any bit of data

I'm not sure how to explain...I'm using something called LoopEdge, I connected my PLC through that, and I have a datahub, which is where the data is coming from.

Here is my flow, if it helps explain at all.
image

Thank you for that link, I'll read it and try to do what you suggested.

Sorry, I realise this is off-topic, but I have to ask... why is your Function node a round circle? Where did you get your Node-RED from?

4 Likes

Assuming it is attached to the msg payload:

msg.payload.values[0].value

The thing that catches a lot of people out is the fact that the inner values object is wrapped in an array which you have to deal with ([0]) before you can access the data.

I'm using something called LoopEdge with LoopFlows. Inside LoopFlows there is a way to add the node-red library. I guess this function node is part of LoopFlows rather than node-RED, but I believe (and hope) it works in the same way. Maybe I'm totally wrong... (I was suddenly given a project and I don't really have any idea about LoopEdge or node-RED :sweat_smile: I'm trying to learn but I'm limited for time, so I appreciate this forum...)

Thank you! This solved my problem. I appreciate it a lot!

Looks like a heavily modified version of an early node red...

https://docs.litmusautomation.com/plugins/servlet/mobile?contentId=10000254#content/view/10000254