I'm currently facing a challenge in parsing a dynamic and complex nested JSON file. My goal is to locate a specific key within an array, but I've encountered an issue with the following snippet of 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 occurs in the line that reads:
"var event". If I change it to:
var event = json['features'][0]['properties']['event'];
The code only captures data from the initial block of the array, rather than conducting a comprehensive search throughout the JSON structure. I aim to scan the entire JSON file for arrays where 'event' matches 'Special Weather Statement' (or any user-specified event).
Once identified, my objective is to organize the results based on the most recent event timestamp found within the 'sent' key of that particular JSON data.
Below is a simplified representation of the JSON schema:
{
"@context": [
// context details reiterated here
],
"type": "FeatureCollection",
"features": [
// detailed feature information provided here
}
// additional features follow
Your assistance or insights into resolving this matter would be highly appreciated. Thank you!