Nested JSON in R, can't unpack weather data

100 Views Asked by At

I am trying to unpack weather alerts from NOAA.

https://api.weather.gov/alerts

library(jsonlite)
#API
string = "https://api.weather.gov/alerts"

#import json and flatten
json_data <- fromJSON(string,flatten=TRUE)

#get names
names(json_data)
[1] "@context" "type"     "features" "title"    "updated"

#extract the features
final_data <- as.data.table(json_data$features)

This results in a table that I can grab specifics about each alert from. However, some results are nested further. For example:

head(final_data$properties.geocode.UGC,1)
[[1]]
[1] "AMZ732" "AMZ741" "AMZ715" "AMZ725" "AMZ712" "AMZ710"

I want to unpack these and pivot the table, so every row should be:

warning | properties.geocode.UGC
storm   | AMZ732
storm   | AMZ741
storm   | AMZ715
storm   | AMZ725
storm   | AMZ712
storm   | AMZ710

How might I do this? Would it involve unlisting that column?

1

There are 1 best solutions below

1
On

It sounds like you don't need GeoJSON so we'll work with the JSON-LD endpoint:

httr::GET(
  url = "https://api.weather.gov/alerts",
  httr::accept("application/ld+json")
) -> res

x <- jsonlite::fromJSON(rawToChar(httr::content(res)))[["@graph"]]
x <- cbind.data.frame(x, x$geocode, x$parameters)

x$geocode <- NULL
x$parameters <- NULL

colnames(x) <- make.names(colnames(x), unique=TRUE)

x <- tidyr::unnest(x, UGC)

dplyr::data_frame(
  id = x$id, 
  event = x$event,
  ugc = x$UGC
) 
## # A tibble: 1,792 x 3
##    id                           event                  ugc   
##    <chr>                        <chr>                  <chr> 
##  1 NWS-IDP-PROD-3212197-2821047 Freeze Warning         NMZ538
##  2 NWS-IDP-PROD-KEEPALIVE-22857 Test Message           MDC031
##  3 NWS-IDP-PROD-3212196-2821046 Special Marine Warning GMZ650
##  4 NWS-IDP-PROD-3212196-2821046 Special Marine Warning GMZ670
##  5 NWS-IDP-PROD-3212195-2821045 Wind Advisory          UTZ019
##  6 NWS-IDP-PROD-3212194-2821044 Red Flag Warning       CAZ211
##  7 NWS-IDP-PROD-3212194-2821044 Red Flag Warning       CAZ204
##  8 NWS-IDP-PROD-3212194-2821044 Red Flag Warning       CAZ283
##  9 NWS-IDP-PROD-3212194-2821044 Red Flag Warning       CAZ277
## 10 NWS-IDP-PROD-3212194-2821044 Red Flag Warning       CAZ203
## # ... with 1,782 more rows