To illustrate how to parse JSON data we’ll create a short Javascript to parse weather data from the Open Weather Map API.
Setting up the HTTP Device
Create an HTTP device in the Devices
panel.
In the Editor
panel activate the Use URL option for the HTTP device.
Setting up the Graph
Create a compute graph in the Graph Editor
and drag & drop the device to create two nodes: Send GET URL and Receive Message.
The first node is configured to send a GET message to the API by specifying the options directly in the URL:
The second node is configured to receive the response from the server.
If required, this output can be written to a String Variable from the Variables
panel. (Here it is named JSON API).
When a Trigger Event is sent to the Send GET URL node, the second node will receive a JSON object with the weather data and we can use a javascript node to parse its data.
Parsing the JSON Data
This is a sample of what the API returns when queried:
{
"coord": {
"lon": 2.3488,
"lat": 48.8534
},
"weather": [
{
"id": 800,
"main": "Clear",
"description": "ciel dégagé",
"icon": "01d"
}
],
"base": "stations",
"main": {
"temp": 6.73,
"feels_like": 5.33,
"temp_min": 4.97,
"temp_max": 8.54,
"pressure": 1035,
"humidity": 87
},
"visibility": 8000,
"wind": {
"speed": 2.06,
"deg": 140
},
"clouds": {
"all": 0
},
"dt": 1676284536,
"sys": {
"type": 2,
"id": 2041230,
"country": "FR",
"sunrise": 1676271788,
"sunset": 1676308003
},
"timezone": 3600,
"id": 2988507,
"name": "Paris",
"cod": 200
}
Note that the JSON has been indented here to improve readability.
Use the JSON.parse()
method to parse the JSON string into a Javascript object.
You can then call the different properties using javascript .name
notation.
JSON Object literals like the one above are formatted in a string. Take care to set the input to a string.
The following snippet of script details how to access the data from the JSON string:
//Parse the JSON string given on the input
const obj = JSON.parse(in_json);
//
//First JSON Object. Contains another JSON Object with 2 keys/values for Longitude and Latitude
let coord = obj.coord;
out_coordinates = "Longtitude: " + coord.lon + " - Latitude: " + coord.lat;
//
//Second JSON Object. Contains an Array with first (and only) item being a JSON Object with 4 keys/values.
let weather = obj.weather;
out_weather = "Main weather: " + weather[0].main + "\nDescription: " + weather[0].description;
//
//Third JSON Object. Contains 1 key/value
out_base = obj.base;
//
//Fourth JSON Object. Contains another JSON Object with 8 keys/values
let main = obj.main;
out_temperature = "Current Temperature: " + main.temp + " C\nFeels like: " + main.feels_like + " C\nMini: " + main.temp_min + " C\nMaxi: " + main.temp_max + " C\nPressure: " + main.pressure + " hPa\nHumidity: " + main.humidity +" %";
//
[...]
To use the data in an output, the values from the JSON Object must first be stored in a local javascript variable.
This is why they are extracted using lines such as let coord = obj.coord;
Need more help with this?
Don’t hesitate to contact us here.