I'm attempting to parse a dynamic complex nested JSON file, where the result would be finding the specific key in an array. Here's a snippet of non-working code:
if (input == 'Special Weather Statement') {
var url = "https://api.weather.gov/alerts?zone=AKZ201";
var slide = SlidesApp.getActivePresentation().getSelection().getCurrentPage();
var res = UrlFetchApp.fetch(url);
var content = res.getContentText();
var json = JSON.parse(content);
Logger.log(json);
var event = json['features']['properties']['event'];
if (event == 'Special Weather Statement') {
var shape = slide.insertShape(SlidesApp.ShapeType.TEXT_BOX, 20, 259, 420, 60);
var textRange = shape.getText();
var description = json['features'][0]['properties']['description'];
var description = description.replaceAll('\n',' ');
textRange.setText(description);
textRange.getTextStyle().setFontSize(17).setForegroundColor('#ffffff').setFontFamily("Libre Franklin");
The problem lies in the "var event" line. If I were to do:
var event = json['features'][0]['properties']['event'];
The code only reads the first block of the array, where I want to fully search the JSON, find where there are arrays that have 'event' = 'Special Weather Statement' (or whatever the event input is).
After, I would want to sort the final output as having the most recent event from the 'sent' key in that specific JSON.
Here's a sample of the JSON structure:
{
"@context": [
"https://geojson.org/geojson-ld/geojson-context.jsonld",
{
"@version": "1.1",
"wx": "https://api.weather.gov/ontology#",
"@vocab": "https://api.weather.gov/ontology#"
}
],
"type": "FeatureCollection",
"features": [
{
"id": "https://api.weather.gov/alerts/urn:oid:2.49.0.1.840.0.1ca69af04faf0efdc58521c0b1b11b10fc0a6cd0.001.1",
"type": "Feature",
"geometry": null,
"properties": {
"@id": "https://api.weather.gov/alerts/urn:oid:2.49.0.1.840.0.1ca69af04faf0efdc58521c0b1b11b10fc0a6cd0.001.1",
"@type": "wx:Alert",
"id": "urn:oid:2.49.0.1.840.0.1ca69af04faf0efdc58521c0b1b11b10fc0a6cd0.001.1",
"areaDesc": "Western Arctic Coast; Northern Arctic Coast",
"geocode": {
"SAME": [
"002185"
],
"UGC": [
"AKZ201",
"AKZ202"
]
},
"affectedZones": [
"https://api.weather.gov/zones/forecast/AKZ201",
"https://api.weather.gov/zones/forecast/AKZ202"
],
"references": [],
"sent": "2022-10-09T14:23:00-08:00",
"effective": "2022-10-09T14:23:00-08:00",
"onset": "2022-10-09T14:23:00-08:00",
"expires": "2022-10-10T05:00:00-08:00",
"ends": null,
"status": "Actual",
"messageType": "Alert",
"category": "Met",
"severity": "Moderate",
"certainty": "Observed",
"urgency": "Expected",
"event": "Special Weather Statement",
"sender": "[email protected]",
"senderName": "NWS Fairbanks AK",
"headline": "Special Weather Statement issued October 9 at 2:23PM AKDT by NWS Fairbanks AK",
"description": "West winds of 25 to 35 mph blowing over a large fetch of open\nwater over the Arctic Ocean and Chukchi Sea will cause elevated\nwater levels potentially 1 to 3 feet above the highest high tide\nand elevated surf along the Arctic Coast from Tuesday through\nThursday. This will likely lead to beach erosion along the Arctic\nCoast.",
"instruction": null,
"response": "Execute",
"parameters": {
"AWIPSidentifier": [
"SPSNSB"
],
"WMOidentifier": [
"WWAK81 PAFG 092223"
],
"NWSheadline": [
"Elevated Surf Possible Tuesday Through Thursday"
],
"BLOCKCHANNEL": [
"EAS",
"NWEM",
"CMAS"
],
"EAS-ORG": [
"WXR"
]
}
}
},
{
"id": "https://api.weather.gov/alerts/urn:oid:2.49.0.1.840.0.fb007fa8f54048b448cba44f41ac214a0aae3bb0.001.1",
"type": "Feature",
"geometry": null,
"properties": {
"@id": "https://api.weather.gov/alerts/urn:oid:2.49.0.1.840.0.fb007fa8f54048b448cba44f41ac214a0aae3bb0.001.1",
"@type": "wx:Alert",
"id": "urn:oid:2.49.0.1.840.0.fb007fa8f54048b448cba44f41ac214a0aae3bb0.001.1",
"areaDesc": "Western Arctic Coast; Northern Arctic Coast; Central Beaufort Sea Coast; Eastern Beaufort Sea Coast",
"geocode": {
"SAME": [
"002185"
],
"UGC": [
"AKZ201",
"AKZ202",
"AKZ203",
"AKZ204"
]
},
"affectedZones": [
"https://api.weather.gov/zones/forecast/AKZ201",
"https://api.weather.gov/zones/forecast/AKZ202",
"https://api.weather.gov/zones/forecast/AKZ203",
"https://api.weather.gov/zones/forecast/AKZ204"
],
"references": [],
"sent": "2022-10-09T06:42:00-08:00",
"effective": "2022-10-09T06:42:00-08:00",
"onset": "2022-10-09T06:42:00-08:00",
"expires": "2022-10-09T16:45:00-08:00",
"ends": null,
"status": "Actual",
"messageType": "Alert",
"category": "Met",
"severity": "Moderate",
"certainty": "Observed",
"urgency": "Expected",
"event": "Special Weather Statement",
"sender": "[email protected]",
"senderName": "NWS Fairbanks AK",
"headline": "Special Weather Statement issued October 9 at 6:42AM AKDT by NWS Fairbanks AK",
"description": "West winds of 25 to 35 mph blowing over a 600 NM fetch of open\nwater over the Arctic Ocean and Chukchi Sea will cause elevated\nwater levels and elevated surf along the Arctic Coast from Monday\nnight through Thursday. This will likely lead to beach erosion\nalong the Arctic Coast Monday night through Thursday.",
"instruction": null,
"response": "Execute",
"parameters": {
"AWIPSidentifier": [
"SPSNSB"
],
"WMOidentifier": [
"WWAK81 PAFG 091442"
],
"NWSheadline": [
"Elevated Surf Possible Monday Night Through Thursday"
],
"BLOCKCHANNEL": [
"EAS",
"NWEM",
"CMAS"
],
"EAS-ORG": [
"WXR"
]
}
}
}
I would appreciate any help or input, thanks!
CodePudding user response:
You can use the sort
method to order the features array, and after getting the first element of the ordered array:
Sample:
var url = "https://api.weather.gov/alerts?zone=AKZ201";
var res = UrlFetchApp.fetch(url);
var content = res.getContentText();
var json = JSON.parse(content);
const orderedEvents = json.features.sort((a,b)=>{
return new Date(b.properties.sent).getTime() - new Date(a.properties.sent).getTime()
})
console.log(orderedEvents[0].properties.event)