How to parse JSON response in REST API | 4MV4D | S01L13
[Music] in order to subscribe to my channel please click here or click here please share comment and like my videos and channel hello guys welcome back to sas word service now this is part of servicenow four minutes video for developers this is the next lesson of series of rest integration in service now in this lesson we will learn about parsing json response from rest api as a developer if you have done integration you must have got the responses from different application in different formats let's say you get the response in json format how can you read how can you segregate that response so that you can populate that data into your current system because you will get the whole body however you have to read the data which have been sent by another system and that's how you call it as parsing the json response so in this video i will show you when you get a response from another system which is in json format how exactly you can parse it so if i go to my personal developer instance so this is my primary instance which is the source instance and this is my destination instance so we will basically fetch some data from this instance and maybe we will create some rackets in this instance in this case you will be able to learn how exactly you update the data or maybe use the data from another system and create racket so maybe update rackets with the help of that response so in order to show you this demo i'm going to use a different table this time as of now whatever demo i showed you we were talking about cisu user table we were talking about incident table or whatever sessions i have taken till now we were always talking about uh these type of tasks table or maybe it's this user table but in this example in this demo i am going to use asset management table so i thought just to show you a different example so that you understand better so in this example i will show you that how this primary instance will keep on checking for the new assets created in this secondary instance and create same assets in this primary instance that means it has to read the data so it is not basically uh that particular instance that means our destination instance or maybe secondary instance is not creating the racket from there it is not doing the outbound from that system that means not from here what we will do we are going to check that means event will be triggered from this instance this is primary and it will check that instance that is secondary that hey do you have new rackets in your system if the answer will be yes then this system will create the rackets as per the data received from this system now in order to do that we will first check the api for for basically the that table asset table in this instance so i will go go to quickly asset table first because i want to show you that for which application i'm talking about so here is the asset management application we have you can see over here and it tracks all the assets so i will go to all assets now here you can see all the assets of your instance now this is out of the box instance and this is personal developer instance so these are all demo data similarly even if i talk about this primary instance i would have similar assets in this instance all as well but in order to differentiate i'm going to create some demo data as well that means just kind of example over here so i will just create few assets and i will try to fetch them in our primary instance and create the data and that's how you will come to know that how you basically read the json response parse it and use it in your primary instance so in that case and and it is just an example guys you might get similar kind of json response from any other system apart from servicenow as well it it should not be just service now instance it can be any instance so in that case if you get those kind of responses how exactly you can parse it how exactly you can use it how exactly you can segregate it that is what you're going to learn today so what i will do i will quickly go to api explorer that's rest api explorer so i will quickly open this why i am doing that because i need to just check the api because i have to create those configurations in my another instance now here i will select basically retrieve rackets from a table and we will keep selecting this get but here i will select the table name and the table name should be the asset table name so i will search for asset so we have this alm asset i will select this one and now i will come little bit bottom now here i can put a query because if i will do the query i will do a get from primary instance it might return all the data so maybe we will put some filters so that we can create over there so what i will do maybe let me let me go to asset table again if i go to all assets i will just create two rackets just to show you how exactly uh you can you can read uh the responses for two new rackets so in that case i will click on new i will select hardware and here i will select the model category and i will select the model and i will just maybe i will provide asset tag as well and this will be maybe aix one two three four just an example i'm giving it so i have saved it so our new asset is created hardware asset is created and you can see some other fields are also populated and what i will do now i will basically create another asset as well we will have two assets overall so here is the latest one and then i will click on new again i will this time maybe let's select hardware that's fine and i can select the category model category maybe computer this time i can select the model let's say dell one that's totally fine and i can just put maybe com one two three four that's a asset tag i'm putting and i will just save it so it is saved i already got some information over here and i will just close it and i will just open the list of assets in a new tab just to make sure that yes you can check the data over here it has already populated some cost but our aim is just to fetch the data in another instance now these two records are not available in our primary instance so let's say this is this is definitely a service now instance but let's treat it as a third party inst uh not let's not treat just as a service now it's a third party uh instance just a third party application let's see and it is a third-party application but as of now i'm just showing you uh as part of the service now instance but it can be any third-party application where your service now instance will hit that instance and fetch the data so similarly uh we have these these uh do two applications so our one application is serviced now and this is another third party application so what exactly we will do now these two rackets are not available in our primary instance so the goal is basically the requirement is that whenever a new asset is created in this particular application i need to create with same information in that primary instance that's a requirement how can i achieve it i have to do a get query so for that what i will do i will just quickly check this api explorer first because i have to put those settings but before that i will put some filters which i can use as a parameter how exactly so i will do created created maybe in the last 20 uh maybe two hours let's do two hours so whatever assets they got created in last two hours i will i will get that data in my primary instance that means in my primary service now instance so in that case what i will do i will just copy this query i will paste it over here and i will just do i will just make the limit as 10 and i will i think we can just keep as it is and i can just click on send if i will do that this will basically return the response over here and this will have two rackets and this is in json format that's a response body now in order to basically hit this system from this system i have to create a rest message but how exactly this system will come to know that that another service service now instance or maybe another application has created some some new rackets so basically we will create a scheduled job so that the system can keep on check that hey did you create the new racket in last two hours so it will say yes i do have three or four rackets or whatever records and then it will return the response so let's create the rest message first and then we will create the scheduled job so that we can fetch the data regularly from another system so in that case i will go to rest method and we already have this integrate with another service now so i can just click on it so you can see we already have all the configurations available over here and we did default get we also did create incident but now this time we are going to fetch the data from asset table so i'm going to create a new method now so let's create a new method and here i can just do get assets that's it and i can just select get here i have to put the end point now as of now we were just doing the integration for incident table or maybe the user table but now i have to change the end point so i will go to my destination instance that means the secondary instance i will just fetch this url so let me just fetch the url so this is the end point i will copy and paste it over here authentication i can just inherit from the parent so i don't have to change anything and if i come over here in http request i can still put the accept one so i can just put accept and here i can put the value application slash json because we are going to get the response in json format and in query parameter i think let's do one thing because we have to add the query parameter what that query parameter we have to put so that query parameter is basically this one syspem query if i will add it over here and just save it and i can just copy rest of the value over here save and i i have another parameter as well that's a sperm limit so i will just go to till end so what i can do i can just copy this sperm limit add another parameter and i can give the value as 10 and what i can do i can just remove the whole data from here and i can just keep till this alm underscore asset that's it that's our main endpoint so i will just save this it is saved so our new method for getting the assets is created so it will fetch the assets from another instance and if i come over here so i have these data so what i will do i will do a quick test whether i am able to get the data or not so first i will do one thing i will click on test and if you will see yes we are able to get the data so you can see we are getting this data over here that means we are able to get the response successfully for that asset table and you can see we got two rackets now you will find some online applications which will basically format the json data whatever data you have it will try to format it so that you understand the format of that data and then you perform the coding then you read and segregate or get the values from that json format so in that case i will just copy this one because we know as of now we will get the basically uh the same response every time because we mentioned the condition as created in last two hours so that's what we mentioned so in that case here is the bookmark i have already saved so i will click on it and the website's name is freeformatter.com and i will just paste the same link what i can what you can see over here i will paste the data over here the whole response whole body and i will just click on this format json once you will do that you will be able to read that how how exactly the format we got so you can see we got result we got this data so if i will like collapse this you can see you will see that we got lot of data over here okay it is still sending it is not using the parameter it is sending the whole data guys you can see here it is sending all the all the rackets of another system and that's what we don't need but it's a sperm query created on last two five two zero two i think it is not following the exact value which we really want so maybe we can do one thing and if i come over here and i have this basically syspem query over here so i can just copy that value maybe i will just copy the whole whole url and i can just go back because it is sending the whole data that's something we don't need right now because i am just looking for specific data that is created in last two hours and that's a really important to check that json body that what kind of response you are getting so in that case if i come over here i think it should be good but let me just put the data over here and i will just remove this till here and we have suspend view i will remove that and i think we should be good if i save this i click on save i i know the reason why it is not basically uh using this parameter the reason behind it and i will i will quickly tell you why because i know the reason because we are putting the right uh value over here it's still not sending the right parameter so if i click on test again the reason behind it because you cannot write anything in this particular field more than that value so it is basically cutting the parameter and that's really that's the reason it is not sending the right parameter to that particular instance that means that that instance is not getting the right parameter i think it's a new learning i would say so uh but that's fine overall we know that we are getting the response and we will resolve it because we don't need this test over here because i just wanted to test whether we are able to get the response or not so i will just discard it and i will come over here and i think we can just go to our instance again and i will just copy the whole uh parameter again i will just copy this one till maybe here copy i will go over there and i can just put the parameter value and i just need to remove till here i will just remove this so we have whole parameter value so i can just click on save so i'm not going to do test now because we have already tested that we are getting the response but i will show you the data will be squeezed how you will see quickly so we have created the method now and now i will just click on this preview script usage because i have to put this script in our schedule job which will keep on check for the new racket so you can see i will just copy this one close and now i will go to schedule job i will go over here so here i will click on new i will click on this because we have to run the script frequently i will make it inactive because i don't want to run this script every time this is my personal developer instance but if you might get this kind of scenario then you can make it active as per your use case i will just mention over here get assets from dev seven zero seven three one and i can just put the script over here so it will call the same rest message integrated with another service now it will call this method which we created and i think everything should be good because all your profile is set everything is set the only thing is we just need to check whether we are able to log the body or not so let's do that so if i just put because as of now uh you're not overriding anything so whatever data whatever configuration you have in your rest message it will send as it is but if you want to override for example maybe authentication profile maybe the parameter that's something you can do so but for example for now i'm just doing gs dot log and i'm just logging the response for now and i will do response body and i will start with parse demo but that's what we have to read the data but as of now i have not done that yet because i'm just creating the rest message and a skill job but you will see now that how you can read the data from the json body so i will click on save and now i will click on execute and let's see if i will get something so i will go to logs and i can just click on all now here i will go to execute now if i click on execute it will trigger that rest message and if i refresh it absolutely can you see that we got the whole response we got purse demo and i think it is still sending the whole data it is not basically uh i think uh putting that parameter so what we can do we can still use it by entering over here we can do that so what you can do let me let me go to this message and let me just cancel it from here and even the limit let's do that and i can just save it and now i come over here and if i copy the whole url this time let's copy the whole url if i go over here and i just mentioned i'm not overriding the endpoint right now i can do that in my in my this script that schedule job and in basically um schedule job we had it can it can definitely like if i want to overwrite i can do that but i'm not doing it right now so what i can do i can just keep the same end point that's totally okay and i will click on save let's see what kind of response we will get but before that i just want to uh copy this whole data maybe so i will just copy i think i think it has it has a lot of data so i will just show you over here maybe if i will put it because i want to show you the difference that if i am changing the configuration will it change will it be okay i have to remove the sparse demo i have to click on format json again and this time you can see again it has a lot of data all the rackets but i don't want that and i am going to basically execute now that i will do so i'm going to reload it and i'm going to click on execute and if i go to logs and i just refresh this you can see we got the data but this time it is squeezed it is not the exact data which is over here you can see a lot of a lot of objects we got a lot of values we got got over here but here is the squeeze data how we got it because now our parameter got worked so in that case i am going to copy this whole data and you will see how many rackets we have got so i'm going over here i'm putting the whole data i am removing that parts demo because i don't want that and i'm just clicking on format json and i will show you you only have two rackets the reason behind it because our parameter worked the query parameter which we mentioned that it should only return those records which were created in last two hours that's it and that's what we created and we got only two records in the system now you can see that we got two rackets how would you be able to read the values for each record that's how you have to use the parsing because we will parse this response that's json and we will loop the this json response and then we will read each value how exactly we will do let's go to over here uh basically we will do it in our scheduled job so i will just go back i'm sure i will reach to schedule job so here is our scheduled job so what i will do now in order to in order to parse the json response you have to start with you will just store that value in a variable so you can create any variable so let's see i'm just creating parsed data equal to i can just mention response body okay i can just start with parse not response body i have to parse the response body but i have to do json dot that's how you parse any json response so if you're getting the json response you have to start with the string syntax has to be the same you can still change the variable that is parse data it can be different but when i'm writing from here it has to be same so let me make it full screen so that you can see better and i'm just going over there so you have this json. parse that's exact syntax you have to use put the brackets here you have to put this variable where you have stored the body the response body which is in json format i will put the semicolon now you have to read each value so what i will do i will basically print those values in the logs because that's the main target i'm talking about that how exactly you can parse the data when you get the response in json format how exactly you can parse that json format so in that case what i will do because i showed you we got two rackets and as of now i'm getting two rackets but you might get multiple rackets so in order to reach basically uh read each racket basically attributes and values then you have to loop uh lube the loop the basically array how you will do that i will show you the reason behind it it is it is a json array so because i i really want you to understand it's a json array because here every here every every like it's a json array where we have uh the object and we have the value basically as well so that's what you are you are keep on iterating that okay let's iterate this one and then iterate this one and you might get multiple values and you have to keep on iterating with the help of you have to keep on checking with the loop it depends what kind of value you want to get so as of now i'm getting two values uh two rackets so i will come over here in this script here what i will do i will start with for now i have to basically write the loop so i will do i equal to zero and here i can do parsed data now where exactly we have that array the array basically starts from our result because result will have multiple values so in that case i will do parse data dot result and i can do dot length that's what it will calculate the length of that array how many values we have because it will check it so maybe that result will have maybe for example two records for now and i will just do i less than that's it because the length of array will be 2 and i want to loop it start with 0 and then 1 that's it so it will not reach to 2 because we only have 2 rackets zero one that's it but if i talk about length perspective we have to to count as part of the length so i will just do semicolon i plus plus and if i come over here what i will do i will show you by logging each records data let's say we will read the asset tag so the name of the field in in the both the rackets will be because field will not change its asset underscore tag how will i print that asset tag for both the rackets what you will do you will do the asset tag is and that value will be you just need to copy here put it over here and you have to do like this because that's the array and you have to iterate with the each object you have over there so here we are just uh looping the value we have so and we have multiple multiple values basically in in that result so in that case basic it will iterate with the whole every object we have in that particular array so what i will do i will just do semicolon over here so we have parse data dot result and we are putting i because it will keep on putting first zero and then it will loop again and put one because that's the array we have so that's how you iterate an array you have to perform the loop so in that case i will just click on save and you will see the magic so asset tag is okay i forgot one thing because i have not put in the name of the field so that's something you have to put and that is asset underscore tag so you will put that result i and then you have to put dot as a tag this will print the asset tag for each record how many records it will iterate only two times zero one and then i'm going to click on save it will save it and i'm sure it will print it so i'm going to execute now if i click on execute let's see what kind of response we'll get so here's the log and we will check i will refresh the list absolutely you can see over here we got com1234 which i will go over here and you will see yes we got the same asset tag and we got this one as well for both the rackets that means it lubed and it printed as well so we used for loop and it is keep on printing now similarly if you have to use uh basically json data whatever body you are getting you have to read it and populate it somewhere that's our next section that how you can populate this data in your one of your different tables because as of now we did not create it
https://www.youtube.com/watch?v=UVBECQnhTew