Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/67.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R-导入和合并多个(嵌套?)JSON_Json_R_Nested - Fatal编程技术网

R-导入和合并多个(嵌套?)JSON

R-导入和合并多个(嵌套?)JSON,json,r,nested,Json,R,Nested,我希望合并150个小JSON文件(所有文件的格式都相同,变量也相同),这些文件是我通过jsonlite导入R的 问题是每个文件都以1的列表形式导入。我可以让一个人转换为dataframe,但找不到一种方法系统地转换所有数据帧 目标是将所有数据合并到单个数据集中 JSON文件中的一个示例: { "data": [ { "EventId": "20020528X00745", "narrative": "NTSB investi

我希望合并150个小JSON文件(所有文件的格式都相同,变量也相同),这些文件是我通过jsonlite导入R的

问题是每个文件都以1的列表形式导入。我可以让一个人转换为dataframe,但找不到一种方法系统地转换所有数据帧

目标是将所有数据合并到单个数据集中

JSON文件中的一个示例:

{
    "data": [
        {
            "EventId": "20020528X00745",
            "narrative": "NTSB investigators may not have traveled in support of this investigation and used data provided by various sources to prepare this aircraft accident report.During the dark night cross-country flight, while at a cruise altitude of 2.000 feet msl, the pilot initiated a climb to 3,000 feet.  A few minutes later, the engine's rpm dropped 200-300 rpm.  The 67-hour pilot increased throttle to check for an rpm response.  Subsequently, the engine lost power, and a forced landing was initiated.  While approaching to land, the pilot noticed trees in front of the airplanes flight path and started looking for another place to land, but couldn't see anything because it was too dark.  Subsequently, the aircraft impacted tress coming to rest upright.  An examination of the engine under the supervision of an FAA inspector, revealed the left magneto's internal gears did not rotate with the engine.  Removal of the left magneto revealed only one of two rubber drive isolators inside the ignition harness cap.    Internal inspection revealed the contact points on the left hand side of the magneto did not open on rotation.  Further examination of the airplane, displayed the ignition key turned to the left magneto only.  The pilot reported to the NTSB investigator-in-charge, that he did not touch any switch while exiting the aircraft.",
            "probable_cause": "The pilot's failure to set the ignition key to the both magnetos position, which resulted in a loss of engine power.  Contributing factors were the failure of the left magneto, the lack of suitable terrain for the forced landing, and  the dark night."
        },
        {
            "EventId": "20090414X14441",
            "narrative": "NTSB investigators used data provided by various entities, including, but not limited to, the Federal Aviation Administration and/or the operator and did not travel in support of this investigation to prepare this aircraft accident report.The pilot was following a highway to the northwest at 10,000 feet mean sea level.  He crossed the mountain pass between 700 and 1,000 feet above ground level climbing slowly.  Once on the west side of the pass, approaching the base of some cliffs, they encountered a strong down draft and the airspeed dropped rapidly and the airplane started to descend. The pilot reports that he attempted to keep the airspeed at 85 knots and climb but, that the airplane continued to lose altitude.  He checked the engine instruments and did not note any degradation of engine performance.  The airplane continued to descend.  The pilot executed a forced landing in approximately the center of the valley ahead of them. The pilot reported that there were no preimpact mechanical malfunctions or failures.  Based on the temperature and pressure readings from the closest weather reporting station, the density altitude at the accident site was about 9,200 feet.",
            "probable_cause": "The pilot's encounter with a windshear/downdraft that exceeded the climb performance capabilities of the airplane."
        },
  • 使用
    fromJSON(file_000.json)
    导入--创建一个“大列表”
  • 导入后,
    df
    library(dplyr)
    图书馆(jsonlite)
    x
    #>事件叙述可能的原因
    #>(chr)(chr)(chr)
    #>1 20020528X00745 NTSB调查飞行员故障
    #>2 ASDFASDASDF NTSB调查飞行员故障
    #>3 asdfafsdf NTSB调查飞行员故障
    #>4 20020528X00745 NTSB调查飞行员故障
    #>5 ASDFASDASDF NTSB调查飞行员故障
    #>6 asdfafsdf NTSB调查飞行员故障
    #>7 20020528X00745 NTSB调查飞行员故障
    #>8 ASDFASDASDF NTSB调查飞行员故障
    #>9 asdfafsdf NTSB调查飞行员故障
    #>10 20020528X00745 NTSB调查飞行员故障
    #>    ..                  ...                ...                ...
    
    test2 <- as.data.frame(file_000.json$data)
    test3 <- unnest(file_000.json)
    
    library(dplyr)
    library(jsonlite)
    x <- '{
        "data": [
            {
                "EventId": "20020528X00745",
                "narrative": "NTSB investigators",
                "probable_cause": "The pilots failure"
            },
            {
                "EventId": "asdfasfasfasfasdasdf",
                "narrative": "NTSB investigators",
                "probable_cause": "The pilots failure"
            },
            {
                "EventId": "asdfafsdf",
                "narrative": "NTSB investigators",
                "probable_cause": "The pilots failure"
            }
        ]
    }
    '
    files <- replicate(10, tempfile(fileext = ".json"))
    for (i in seq_along(files)) cat(x, file = files[i])
    dplyr::bind_rows(lapply(files, function(z) {
      jsonlite::fromJSON(z)$data
    }))
    
    #>    Source: local data frame [30 x 3]
    #>
    #>                EventId          narrative     probable_cause
    #>                  (chr)              (chr)              (chr)
    #>    1        20020528X00745 NTSB investigators The pilots failure
    #>    2  asdfasfasfasfasdasdf NTSB investigators The pilots failure
    #>    3             asdfafsdf NTSB investigators The pilots failure
    #>    4        20020528X00745 NTSB investigators The pilots failure
    #>    5  asdfasfasfasfasdasdf NTSB investigators The pilots failure
    #>    6             asdfafsdf NTSB investigators The pilots failure
    #>    7        20020528X00745 NTSB investigators The pilots failure
    #>    8  asdfasfasfasfasdasdf NTSB investigators The pilots failure
    #>    9             asdfafsdf NTSB investigators The pilots failure
    #>    10       20020528X00745 NTSB investigators The pilots failure
    #>    ..                  ...                ...                ...