正在处理 JSON 个文件并将其下载到特定文件夹中

processing and downloading JSON files into specific folders

我有一些数据看起来像:

  index                                                                                                                          title
1 2.1.1                                                                                           Indicadores de renta media y mediana
2 2.1.2                                                                                            Distribución por fuente de ingresos
3 2.1.3                  Porcentaje de población con ingresos por unidad de consumo por debajo de determinados umbrales fijos por sexo
4 2.1.4 Porcentaje de población con ingresos por unidad de consumo por debajo de determinados umbrales fijos por sexo y tramos de edad
5 2.1.5   Porcentaje de población con ingresos por unidad de consumo por debajo de determinados umbrales fijos por sexo y nacionalidad
6 2.1.6       Porcentaje de población con ingresos por unidad de consumo por debajo/encima de determinados umbrales relativos por sexo
                                             link provincia                            provincesFolderLocations
1 https://www.ine.es/jaxiT3/Tabla.htm?t=30656&L=0  Albacete /home/bscuser/Escritorio/mobility/data/INE/Albacete
2 https://www.ine.es/jaxiT3/Tabla.htm?t=30813&L=0  Albacete /home/bscuser/Escritorio/mobility/data/INE/Albacete
3 https://www.ine.es/jaxiT3/Tabla.htm?t=30657&L=0  Albacete /home/bscuser/Escritorio/mobility/data/INE/Albacete
4 https://www.ine.es/jaxiT3/Tabla.htm?t=30659&L=0  Albacete /home/bscuser/Escritorio/mobility/data/INE/Albacete
5 https://www.ine.es/jaxiT3/Tabla.htm?t=30660&L=0  Albacete /home/bscuser/Escritorio/mobility/data/INE/Albacete
6 https://www.ine.es/jaxiT3/Tabla.htm?t=30661&L=0  Albacete /home/bscuser/Escritorio/mobility/data/INE/Albacete
                                                          json_link
1 https://servicios.ine.es/wstempus/js/es/DATOS_TABLA/30656?tip=AM&
2 https://servicios.ine.es/wstempus/js/es/DATOS_TABLA/30813?tip=AM&
3 https://servicios.ine.es/wstempus/js/es/DATOS_TABLA/30657?tip=AM&
4 https://servicios.ine.es/wstempus/js/es/DATOS_TABLA/30659?tip=AM&
5 https://servicios.ine.es/wstempus/js/es/DATOS_TABLA/30660?tip=AM&
6 https://servicios.ine.es/wstempus/js/es/DATOS_TABLA/30661?tip=AM&

我正在尝试 map 使用 map2 超过两列。我想转到 json_link 列中的 link 并使用 jsonlite 包中的 fromJSON 来读取 JSON 文件。然后我想将该文件保存到列中指定的位置 provincesFolderLocations.

我正在考虑类似于以下内容(不起作用):

downloadAndStoreJSONData <- function(jsonLink, provinceFolderLoc){
 JSON_in = fromJSON(jsonLink)
 JSON_out = write_json(JSON_in)
}

DATA %>% 
  map2(json_link, provincesFolderLocations, ~downloadAndStoreJSONData(.x, .y))

此外,我不想只保存它收集的 JSON 数据,而是想 bind_colsDATA,所以我保留了所有信息。然后我想将这个最终数据框保存为 JSON 文件。

数据:

    DATA <- structure(list(index = c("2.1.1", "2.1.2", "2.1.3", "2.1.4", 
"2.1.5", "2.1.6"), title = c("Indicadores de renta media y mediana", 
"Distribución por fuente de ingresos", "Porcentaje de población con ingresos por unidad de consumo por debajo de determinados umbrales fijos por sexo", 
"Porcentaje de población con ingresos por unidad de consumo por debajo de determinados umbrales fijos por sexo y tramos de edad", 
"Porcentaje de población con ingresos por unidad de consumo por debajo de determinados umbrales fijos por sexo y nacionalidad", 
"Porcentaje de población con ingresos por unidad de consumo por debajo/encima de determinados umbrales relativos por sexo"
), link = c("https://www.ine.es/jaxiT3/Tabla.htm?t=30656&L=0", 
"https://www.ine.es/jaxiT3/Tabla.htm?t=30813&L=0", "https://www.ine.es/jaxiT3/Tabla.htm?t=30657&L=0", 
"https://www.ine.es/jaxiT3/Tabla.htm?t=30659&L=0", "https://www.ine.es/jaxiT3/Tabla.htm?t=30660&L=0", 
"https://www.ine.es/jaxiT3/Tabla.htm?t=30661&L=0"), provincia = c("Albacete", 
"Albacete", "Albacete", "Albacete", "Albacete", "Albacete"), 
    provincesFolderLocations = c("/home/bscuser/Escritorio/mobility/data/INE/Albacete", 
    "/home/bscuser/Escritorio/mobility/data/INE/Albacete", "/home/bscuser/Escritorio/mobility/data/INE/Albacete", 
    "/home/bscuser/Escritorio/mobility/data/INE/Albacete", "/home/bscuser/Escritorio/mobility/data/INE/Albacete", 
    "/home/bscuser/Escritorio/mobility/data/INE/Albacete"), json_link = c("https://servicios.ine.es/wstempus/js/es/DATOS_TABLA/30656?tip=AM&", 
    "https://servicios.ine.es/wstempus/js/es/DATOS_TABLA/30813?tip=AM&", 
    "https://servicios.ine.es/wstempus/js/es/DATOS_TABLA/30657?tip=AM&", 
    "https://servicios.ine.es/wstempus/js/es/DATOS_TABLA/30659?tip=AM&", 
    "https://servicios.ine.es/wstempus/js/es/DATOS_TABLA/30660?tip=AM&", 
    "https://servicios.ine.es/wstempus/js/es/DATOS_TABLA/30661?tip=AM&"
    )), row.names = c(NA, 6L), class = "data.frame")

我可以使用以下方法获取单个 JSON 文件:

(收集每个观察值大约需要一分钟)

然后将列绑定在一起。

jsonLINK <- DATA$json_link[4]
JSONin <- fromJSON(jsonLINK)
DATA_Final <- bind_cols(JSONin, DATA[4, ])

提供写入数据的位置 write_json -

downloadAndStoreJSONData <- function(jsonLink, provinceFolderLoc){
  JSON_in = fromJSON(jsonLink)
  write_json(JSON_in, paste0(sub('\?.*', '', basename(jsonLink)), '.json'))
}

purrr::map2(DATA$json_link, DATA$provincesFolderLocations, downloadAndStoreJSONData)

其中 paste0 创建要保存的 json 文件的文件名。

paste0(sub('\?.*', '', basename(DATA$json_link)), '.json')
#[1] "30656.json" "30813.json" "30657.json" "30659.json" "30660.json" "30661.json"