Let's now create an automation using REST in JSON format.
Most web application uses REST over SOAP like Facebook and Hubspot.
Let's get started.
In the WebAPI tool, select HTTP Client. This will open the HTTP Request Wizard. Let's
use OpenWeatherMap as an example. Let's use the REST API in JSON format.
Let's click on this and let's copy this link.
Then we'll paste the link in the Endpoint field.
As you can see the link already includes the parameters (?q=London,uk)
So if we run the Preview Response, it will give us result.
Now, if we remove the parameters and run it, it will give us an error message.
Let's go back to the Request Builder tab. Let's add the parameters manually by clicking
the "Add Parameter" button under the Parameters dropdown.
[?q=London,uk] Remember the extension of our web service
URL? the letter "q" is the parameter name. So we
will put it here.
The Parameter value is what's next to the equal sign. That will be "London,uk"
For the Request Method, we will use "GET"
since we will be fetching data from this service.
There's no Attachment for this one and no Authentication since this API is public.
Ok. Let's preview the response.
As you can see, the Request URL automatically adds the parameters at the end.
The structure of JSON has some similarities
with XML format. The root element is located in the outside
part of the structure and it encloses the child elements under it.
Let's generate this activity.
Alright. Let' go ahead and review the values in the Properties panel.
In the Input section, here we have the URL for the service.
The Method, which is currently set to "GET". and here's the parameters which currently
have a default values.
And here we have other settings like advanced
authentication which are mostly required by commercial web applications.
In the previous video with XML, we have added DeserializeXML activity. There is also an
equivalent activity for JSON which is added automatically by the wizard.
We'll rename the output variable for the HttpClient. Let's put in "RESTResponse". Let's change
the scope to main.
It automatically changes the INPUT variable for the DeserializeJson activity.
Let's rename the output variable of the JSon Object to "jsonDocument". Let's also change
the scope to main.
Let's add a WriteLine activity. Let's put
in the variable of the string output of the JSON activity which is "RESTResponse".
Let's run it.
So here's the output in string format retrieved from the OpenWeatherMap.
Let's say, we wanted to retrieve specific data from this content.
[show content in notepad] If we want to capture the temperature in a
certain area, we can retrieve it by using its root and child element. In this case it
will be under "main", "temp"
Let's add another write line activity.
The format will be:
the jSOn object which is "jsonDocument" then we enclose the root element
jsonDocument("main") next will be the child element
jsonDocument("main")("temp") then we convert the output to string format
by adding ".ToString" jsonDocument("main")("temp").ToString
Let's run it.
There's the result!
Well, it's currently in Kelvin, let's do a simple math by converting it to Celsius.
The formula for celsius is equal to Kelvin minus 273.
We can only perform math calculations if the current value is in a number format.
We'll use the keyword "cint" to convert the current value to integer then we can subtract
273 from it.
Let's remove first the ".ToString" keyword. Then, we'll enclose the current statement
into a cint. That's the time we can put in "-273" at the end.
cint(jsonDocument("main")("temp"))-273
Then we enclose it again so we can convert the output number back to String
(cint(jsonDocument("main")("temp"))-273).toString
That's it!
Let's run it.
Perfect! The temperature now in London is 10 degree celsius.
There you have it. Say good bye to a costly and slow process of modifying API codes and
use UiPath Web API Automation at your advantage!
Thanks for watching!