https://www.automationdirect.com/brx
(VID-BRX-0032)
This video is part 3 of a 3 part series that details the needed information to setup and use JSON in the do-more BRX PLC.
This video details how to import JSON data and pull out the information needed from the packet.
Our FREE Practical Guide to Programmable Logic Controllers eBook: https://www.automationdirect.com/ebooks/plc-handbook
now that we know how to build a json record let's learn how to parse some data from a json record using the json parse instruction and do-more designer and a bricks plc the purpose of a json parse instruction is ultimately to get a value you need from a json record if the json record has nested objects it will take multiple json parse instructions to get what you want because the instruction will only search the top level it will not drill down into all the nesting looking for what you want each individual json parse instruction you use extracts data in one of three ways the first way is it extracts a single value from a name value pair in an object based on searching for the name in other words it will search the top level objects in a json record looking for the name you specify when it finds it it gives you the value portion of that name value pair this implies of course that you already know the name of the name value pair that you're looking for so for example if the record looks like this containing two name value pairs and you tell it to look up latitude it will search the object find latitude and return its value of minus 16.92 the second thing you can do is exactly the same as the first thing it can do except the search is based on a zero-based index number instead of based on the name this may be useful if you don't know the name or you're iterating through all the top level objects for some purpose so again if your record looks like this you can get the same value you got before only this time you tell it to look for index 1. it will look at the top level objects only until it sees index 1. having found the object at the index location 1 it will now return the same value you got before minus 16.92 but not only that optionally you can also make it give you the name of the value it found the third thing it can do is extract a single value from an array of values based on a zero-based index number so if your array looked like this and you told it to find number two it will do so and give you the value of 362. but that's all well and good if it works but what happens if you receive a malform json record or the data wasn't arranged in the way you anticipate it or perhaps you don't really know what the json record is supposed to look like what then well the json parse can optionally give you a status double word to help you out let's say we pick d1000 as the status double word what will it tell us the upper word of this double word will return a code but the thing is the code can be positive or negative if it is positive then the lower word is divided into two bytes representing the input type code and the value type code more on that in a minute if however the code is negative then the lower word by itself will indicate the character position in the json record that it saw a problem how would you look at these individual pieces that's easy you simply use the proper suffix and cast that value so you can see here that colon w1 means the upper word of d1000 and colon ub1 and colon ub0 mean the unsigned bytes of 1 and 0. similarly the colon w0 means the lower word of d1000 so what do these individual values mean in this double status word first and most importantly a zero in the code word means your json parse was unsuccessful however the json record that it searched was okay no fatal errors were found in other words it was well well-formed if it contains a 1 in the code word then your json parse worked if however the code word is negative then you can see here the various meanings these meanings of course are given detail in the help file for json parse but in this summary box you can see the various problems encountered but back to the positive code the input type byte tells us what type of json record it detected when it did the search for what you wanted a zero here means that there is some syntax error like a missing colon or something the value type byte tells us what kind of value is extracted armed with this information you can now write very complex ladder logic to figure things out if need be that involves some tedious work but it can be done when required with this abundance of information if however you know exactly what kind of json records you're parsing which is the case most of the time then of course you don't need to know any of this but it's there if you need it so how do we go about extracting data from say the json record shown here if what you're after is on the top level a single json parse will suffice for example the date here designated as dt is a name value pair on the top level a single json parse can search the json record for dt and find the value and store it wherever you want how do we know what is on the top level by the indentation the top level objects are only indented once however if what you want is down a level or two then multiple json parse instructions are required for example if we wanted speed we could not get it with one json parse because it's actually stored under wind thus in this case we need two instructions the first to get wind which would be the entire string contained in the curly braces and the second to get the value of speed from that notice speed is indented not once but twice in our specific example which we'll show you in ladder logic we're going to extract some data from a json record provided by a popular weather api and we're going to use that data to make a weather report for a certain city we'll get that json record using a do-more bricks html command ultimately we will extract description temp date time zone and name as here are all highlighted first we want to get the date time which is dt this value is in 1970 epoch format that is the number of seconds that have passed since january the 1st 1970 but is also in universal time so we have to deal with that as well the dt is on the top level so the first json parse will get the value and we'll store it in d1 we'll then use an epoc to date instruction to convert that value to a date time and we'll store that in user date time structure udt0 since it's a universal time we'll use another json parse to get the time zone this value will help us adjust to the proper time this time zone is also in seconds we'll store this value in d2 then we'll use the dt offset instruction to adjust the date time in udt0 with the time zone value that we have stored in d2 and we'll store that final date time in udt1 now we have the proper date and time for the time zone of our city next we want to get the current temperature for that city this is in temp but notice it is stored inside of main also notice this value is in kelvins another issue we'll have to deal with we'll use a json parse to get main first and store that in string sl0 another json parse will search sl0 and retrieve the value from temp and we'll store that value in r0 we'll then use a math to calculate fahrenheit and store that in r1 we now have the date and time and temperature of our fair city next we want the description but it's located inside of an array notice the square brackets above and below the green highlight notice also that the description which is what we're after is indented three times the weird thing is in this case the array has only one value a single object highlighted in green now normally an array would contain multiple values or objects separated by commas but it's not so in this case further this array is inside of weather thus it is going to take three json parse instructions to get what we want so we'll use one to get weather we'll store that in sl1 then we'll use the second one to get the only value in that array thus it will be at index 0. we'll store that in sl2 then we'll be able to get the description out of sl2 with a third json parse we'll store that in sl3 we now have a description of the weather of our city finally we'll get the name of the city this value is on the top level this json parse will store the name in sl4 in the end we'll have the date time location description and temperature of the weather we ask for using the data highlighted at the right all stored in memory locations and properly adjusted so that we can make use of it in our brix plc this will be used to make our weather report once we have that report we could for example send it in an email to some interested party or whatever lastly as an added bonus we'll demonstrate how we can get various objects from the top level storing the value in sl11 and the name in ss3 all by using a zero-based index we'll use v0 instead of searching by name the numbers stored in v0 will index the top level objects there happen to be 13 top level objects in this particular json record as you can see by the underlines indicated here now let's see how this is done in letter logic okay i have a main here that i have done in ladder logic you'll notice i have a json parse program that will be started with c1 so i turn on c1 and i've got this program running i pull it up and the first rung here we have a c10 that's going to go out and use an http command instruction and get some data from api.openweathermap.org right now just know that it goes and it pulls this website and gets some weather back in a json data format now it's going to stuff that into a json data 7 string each string in that block is 1024 characters long so when i execute this c10 you can see it goes out and gets the data here's the data right here and you can see that the length is 463 bytes long so that was quite a bit longer than the longest standard string in the do-more which is sl which is 256 characters so so now we'll use this really nice tool over here and we'll go to the wrench and pick json pretty and you can see here's the data as it came in but over in the right pane you can see how i put the data in a nice format so you have all of these objects inside of this that we're going to parse out and get what we want so we close this guy out so the next thing we're going to start the parsing so here you can see i'm going to go out and [Music] execute this parse on that string we just got i'm going to look for the dt parameter and when i get it i know it's going to be a numeric value if i didn't know it was going to be a numeric value i would put it in text okay because this is where i could store that but down here you can see i know it's going to be a numeric value and i'm going to store that numeric value in d1 so that's what this json parse is about so when i hit c 11 it goes out and gets that value now you know let's go up here and let's just verify it let's look here at this string again and we'll pull this down so you see it searched through this record the json parse did until it found that there it is and that's the data you see that it's a number and that's exactly what it stuffed into d1 let's open this guy up again and we'll look here the uh return results that's that status word i told you about i chose d1006 and so you can see when you got status turned on you can see that it intelligently divides it up let's get the cursor off of there and it even interprets it for you and tells you uh what those values are now let's see what we're going to get next so here's c12 i'm going to take this time value that i got which i told you was in 1970 epoch format and i'm going to convert those number of seconds that have passed since january 1 1970 and i'm going to convert it using this epoch to date time instruction and it will convert it and stick it into user date time zero so when i execute that you can see that it converted these seconds into something actually useful but remember that this is universal time so c13 what are we going to get with c13 we're going to go get the time zone all right so let's execute that one now that i have the universal time and the time zone then i can go down here and i can use this dt offset instruction and i'm going to take that universal time that's there adjust it by this d2 number of seconds and stuff that into udt1 so when we do that what do we get now that give us the actual time we were after next is we're going to hunt for main now this one if you'll remember i know it's going to be a string and so i'm going to grab it and stuff it into sl0 so let's watch that happen c15 there you can see it's showing up in sl0 now this is a little bit tricky so what i'm going to do is i'm just going to go here and it's always going to point to this json input string okay i know right now that this sl 0 is going to contain some json data but the instruction doesn't know that each time so we'll hit this and pull this guy down here and i'm just going to go up here and manually enter sl0 so when we told to get main before i hit enter here it should grab this it should find it and give you this data right here so let's see what happens when i hit the enter button here and there it is so see it went into and found main and it grabbed exactly that piece that we wanted it to so that's what this is right here so we'll scroll this up a little bit more and now we'll go to this rung here now once i got main i got that piece out of there then it was a piece that i wanted out of that which had the temp i was after the temp value again let's go look at this with this json pretty so here you see that's the main chunk that i pulled out and i'm looking for this value right here so that's what i'm telling this next json parse to do is to grab that temperature value again i know it's going to be a number and so i also know it's going to be a real number so i'm going to stuff it in a real value so you can see right here this one gets the string we just did tells it to look for temp and i know it's going to be numeric stuff it into r0 so c16 there it is that temperature value is in kelvins so my next instruction down here is just going to do some math to convert the kelvins to fahrenheit there it is 78.24 degrees fahrenheit next json parse i'm going to take that big data again and i'm going to go get the weather remember this one is going to take three to get what i want out of this one so we'll pull this guy back up again using json pretty i'm after something inside weather is the description so so you see it's going to grab this weather but remember to see this this square bracket here indicates this is an array from this bracket to this bracket and the problem is of course this array only has one object in it so i've got to get weather which is going to pull this array up then inside this array i've got to get this object and then inside this object i'm after this description so it's actually going to take one two three json parses to get exactly what i want so let's do that one let's do the first json parse i hit that guy and now it should get that array out of there you can see it starts with that bracket so we got that array so you can see since i already know that i tell it to get that string and i tell it zero based array field index get the first thing in that array i know it's zero i know it's going to be some more text and we'll stuff that into sl2 so we execute this one now you can see see how it removed the brackets or the square brackets so now we just have that first object out of that array and there it is so that is now an sl2 c20 look for description uh find i know it's going to be text stuff that into fl3 here we go there it is clear sky so i got what i wanted out of there now i'm done with that one so what's the next thing i'm going to do well the next thing i'm going to do is i'm going to go get the name so if we execute that we got it so now just to sum up i kind of put all the things i wanted right here in a row and you can see my udt1 it's the year 2020 october the 21st it is wednesday and the hour is 15 which is 3 o'clock in the afternoon 3 19 29. the city is bluff city it's clear skies and it is 78.24 degrees fahrenheit so i could actually use a something like a string print or an email instruction and put this data in some english and send it to someone if i wanted to so that's how you use the json parse instruction to get the data out that you want now in this little demonstration all i'm going to do is i'm going to use that long string again the original string i got from the http command and i'm going to just show you how you can also index through these top objects so remember here's the long string and what the json parts will allow you to do is it'll all the top objects you can just iterate through them so to do that you can see uh this is the original string that's the input i'm going to use a zero base array and i'm going to use v0 to do that and uh whatever field name it finds at this index it'll stuff that name into ss3 i know whatever it gets i'm just going to stuff it in a string because sometimes it's a value sometimes it's a string i'm going to stuff it in a string all the time so we're safe there put it in sl 11 and then i'm going to form a string print that's just going to to have the index that i used a dash the name a colon and then what it found and stuff that into sl12 that's why i'm doing that and then of course i'll automatically increment v0 to its next value and also reset this so right now it should be set to zero so when i hit this you see it gets chord and now that isn't the length of that one is actually 39. so if i go down here and read that you can see that's what it found hit this again and you see it used index 1 and it found weather and if i want to see what they're actually there is you can see it grabbed all of that there's the base main visibility wind clouds dt sys time zone id name and coordinates back to zero that's how you would use json parse center brix plc to get what you want out of a json record thanks for watching you
Voted #1 mid-sized employer in Atlanta
Check out our
job openings