Working With JSON Data in Node Red

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
this video we can look at using JSON data within node-red now Jason stands for JavaScript object notation and it's an open standard file format uses human readable text to transmit data object consisting of attribute value pairs that's the wiki definition packaging data is JSON it's a common way of sending data between systems it's also a common way of storing data on systems if you're reading configuration files into node-red you might find a lot of the time they're in JSON format so you need to convert them into JavaScript and Jason although he started life with JavaScript is not restricted to JavaScript there are functions in all the major programming languages for diverting from JSON format to an internal format now in JavaScript there are two functions that you use they are adjacent or parse and json dot stringify and the json dot pass converts from a json string into a javascript object so in data is a JavaScript object and the json string you pass to the adjacent or pass function and you get a javascript object back and the opposite is the string to phi function where you give it a javascript object and it gives you back a JSON string and that's usually when you're sending data out of the system and the parsing is when you're bringing data into the system and we can look at using these two functions in the function mode in node-red so let's look at our first node read flow and what i've got here is an inject node and a couple of function nodes and debug nodes so we can see the results the first function node is going to create the json data so here we start off with a javascript object here and then we convert it into a json string and put it into the message or payload and then we return the message so that json string gets passed into this function node and this function knows uses the the password we talked about just earlier and then it converts that from a JSON string into a JavaScript object which is P here and then we extract the temperature from there P dot temp into the message dot payload and we return it so we should print out the temperature and let's just go back and we look at the node here the temperature is 30 you can see it here and the humidity is 80 so let's deploy that and we just inject using the inject node and you can see over here that we've got the temperature humidity this is coming out of this node this is converted into JSON format and temperature of 30 which is coming out of this node here as we see here we just put the temperature into the payload and we get 30 coming out now you don't need a function node to create JSON data you can actually create it directly from the inject node but there is a gotcha with it and if I look at the inject node here and we just create a jason and this is actually not creating a JSON string it's actually creating a JSON object if we look at in here there's a topic test on s1 and a temperature of 20 and when I first did this I expected the output of this to be a JSON string and it goes into the function load and the function node the first thing he does expect a JSON string so first thing it does is convert it into an object before it processes it and let's have a look what happens when we do it clear that and then we inject our data and over here you can see here we've got a unexpected token 0 or in the JSON a position 1 if you do a google search on that which I have done here and it basically tells you that you've already got a JSON object so you don't need to actually pass it so what's coming out of this thing here is actually a JSON object so we don't need to convert it from a JSON string into a into a JavaScript object so if I just then remove that I come to nodes and I just take the pain I'd payload directly rather than trying to do a conversion and we deploy that one clear that and do the inject again this time we see our temperature in the temperature this time is 20 okay you might have noticed this node floating around up here now this is a Jason node which will actually convert between a JavaScript object an adjacent string and vice-versa and if we open it up you can see here the action here is to convert between adjacent string an object always convert to adjacent string and always convert to a JavaScript object so if you leave it here it will examine the data and decide whether it's a JSON string or a JavaScript object and do the appropriate conversion ok so let's use it and what I've done here is I've changed it so it's going to convert to an adjacent string an object so it's going to decide what it needs to do and let's drop it into our flow now I've edited this function here so we actually send out the later we don't actually convert it into a JSON string we send it out as a JavaScript object and I've modified this function here back to the beginning so it's expecting a JSON string so it's gonna pass it and I'm just commenting that out okay so no when I deploy it and I inject you can see it's working we got the temperature coming out of here so this node here has converted our JavaScript object into a JSON string which then goes into our function gets converted into a JavaScript object again by the pass function and then we print out the temperature from that javascript object now I've showed you the use of the JSON mode very often I I do the work in the function if you're using the function you might as well just add the extra line to convert it between JavaScript and JSON or vice versa within the function so I actually very rarely use this this node but it's useful to know that it does exist especially if you're not going to use a function node okay let's go on to a second example and this time we're using the inject node and we're going going to send in our JSON data and this time we've got a bit more complicated data we've got the topic which is test s1 and we've got data and the data itself is an object consisting of the temperature humidity so to access this temperature we need to access it as data dot temp and to access the humidity we need to access this data dot humidity so that's done and if we go to our function mode very similar to what we had before I've counted out this because remember the inject now it's gonna send in a JavaScript object knowledge JSON string and we just take that and put it into P the payload then I assign the payload message or payload to P data temp so I'm just extracting the ter if I wanted to extract the humidity I'd go P dated or temp you can also use this format you can see it here I've just typed it in it's using the square bracket format and the key in quotes so data's in quotes and temperature in quotes I could actually use a mixture of these two formats but it's easy to use one format all the other format okay I'm just gonna comment that one out and we've done and now we deploy it and let's run it you can see the temperature is 20 okay we've looked at using the function node and the Jason pass and the stringify functions now we couldn't look at using the change node and we're going to look at using Jason tartar and processing the adjacent data using that so I've got the inject node and this is the same data we were injecting previously this data is a sub object and this time we're injecting it into a change node and we're looking at the resource and I've changed it so as it's showing the complete message object and now I'll show you why in a second and what I'm gonna try and cover here is a couple of the gotchas that I encountered when I was doing this and it can be very frustrating when you you're doing this and it's just not working and I'm trying to explain why there was little common mistakes that I made when I was doing it for the first time and still make from time to time when I'm using this so we're injecting into the change node and if I look at the change node it's just setting the payload I've been testing this so that's there and I'm gonna use the expression here format to process the payload and this lets you put an example message in here and test the the data so let's go and get the JSON data take it from here which is a common thing to do so we just copy it ctrl C it done go back to here edit it and we're going to test and you can see here I'm extracting data temping see straight away the result comes out at 20 if I just go for the data then we finish up with the object here well that's working well now if I deploy this it won't work and it won't work because what's being passed into here is not just that object there it's part of the the payload object so what I really need to do is use is include the payload so I can't extract it just as dated or temporary need to extract it as payload dated attempt and let me show you how to confirm that so this time I'm injecting into the message object and I'm doing this it's deployed first so if I just inject and over here what I want to do now is extract the message object so if you can see this thing here it lets me copy so I click there and it says Val you copied and now this time I go into the change node and they're going to test and I copy that back into here and I'm copying the complete message object now you can see there's no matching result and there's no much of a result I need the payload in front of there and there it is comes out as 20 so now we're done so what we're using is we're setting them the message dot payload two to payload data dot temp and we just pass it on to the debug node so let's clear that and deploy it and will inject again and you can see here the pelo comes out as 20 this time that payload is the object which is that one there and this bottom one here is this one here it's the output of this node here okay so that's so to use the JSON expression editor as well as using a within the change node as well as using a function node to process JSON data and so the last example I want to cover is what we're gonna use there is we will actually send data JSON data out of our examples we've gone through have been processing incoming JSON data converting into a JavaScript object and instructive values from it this time we're going to go create the JavaScript object and then we're going to convert it to Jason string and we're going to send it out so this function here is a function that you might find useful it takes incoming data and it's a verage 'as the data finds the maximum value of the data and it finds the minimum value of the data and that periodically it converted into a JSON string and sends it off so let's add a couple of nodes in here to inject some data into it okay here's our flow and I've created a couple of inject nodes here to inject the values of 2 6 and 8 so our minimum should be 2 and our maximum should be 8 and this is the function node and I'm using two context objects one is called 8 and the other is data to their data is the values that I actually want to be transmitted out and we're gonna convert that object into a JSON string to send out and data to is storing temporary data that I don't need to transmit out so it's why I've separated them to two objects I'm using a 5 second period I did have it set to 15 seconds but for a demo it takes too long if it's amazing on on 15 seconds is when you're waiting for it so we get our date and we get a present time now is d don't get time so we've got a present time and we've got a series of tests here so if the value isn't set if it's undefined then we set it we said to count to zero we've got a sensor ID we got a sensor type you've got the maximum value if it's undefined we've set it to the payload and we've got a minimum value if it's undefined we're against headed to the payload and the average if it's undefined we set it to zero and the start time if it's undefined we set it to now which is the current time and we've got a counter this is stored in data to remember we're not going to send that out and we increment the counter the counter is used for the average so if the count is not equal to zero then we have this if the count is equal zero then the average is basically the payload and I've got a log message here to log some data for debugging now if the payload is greater than the maximum then we set the new maximum to the payload and we do the same with a minimum but in Reverse now we check the time and if the times up then we send this data out we convert our data into a JSON string using the stringify we set the payload to out we clear our data and we clear our data too and we store them back into the context object so we're going to start again on the next cycle and then we return the message now if it's not equal to the if the times not up then we just store our data back into the respective objects and we return and also nothing gets sent out of the node okay so that's the function so we deploy it and now I inject a couple of values in here and you can see over there it's produced and outputs you can see it's maximum age minimum of two and the average of 4.8 but this is a a Jason string and know normally you would be passing this on to mqtt or something else and sending it out or even or restoring it maybe into a into a file now the last thing I wanna show you comes as a result of a question that I had on a video on on YouTube and one of the viewers was trying to decode this data and this is what the data resent me and he was trying to extract I think it was the temperature of the humidity from this Jason object and you can see here we got the topic we got the payload we got quality of service and what he was trying to use was payload or topic payload or QoS setup a payload retain and some of it worked and some it didn't work especially the payload now it's difficult to tell but this is actually this payload is a JSON string and he said unto me before in another context when I was doing some work with Python and I finished up encoding you twice as a JSON string and it actually gets quite confusing so what well I'm going to show you the step so I went through to actually decode this you might find useful and what we're gonna do we're going to use actually the command line and remember node-red is a node application and so you've got node on your machine so we just go into the node command prompt and before I do that I'm just going to copy this and now I finish with that let's go to our command prompt there we go node I know we we paste our object in there and to do this I just right-click here and paste and it goes in there and I just type s and you can see it there so if I were to get the topic I just got s dot topic if I want to get the payload s dot payload quite straightforward now if I want to get the time so let's convert it into a JSON object using the pass function and now we can get the time and we can get the humidity and the humidity is actually part of this object so we have to include that so we go P dot a2 301 dots there we are 33 you might want to play around with that I find it very useful the denote command prompt for testing things out so so that brings a standard video if you've got any comments then please leave them below if you like the video then you click on the like button below and if you want to know know when I post new videos to the channel you can always subscribe and feel free to share it on social media if you use social media until next time goodbye
Info
Channel: Steve Cope
Views: 71,268
Rating: undefined out of 5
Keywords: node-red, JSON, JSON.parse(), JSON.stringify(), JSON node, working with, Stevesinternetguide, tutorial, how to, video, change node, expression editor, jsonata
Id: 24ZY3CEsiow
Channel Id: undefined
Length: 20min 48sec (1248 seconds)
Published: Mon Jan 14 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.