Flatten nested JSON to dataframe in R

与世无争的帅哥 提交于 2021-02-17 05:10:56

问题


I am trying to flatten a nested JSON file from within R,

Here is my current code

library(jsonlite)
json_file <- "json file"
json_data = fromJSON(json_file, flatten = FALSE)
flat_data = as.data.frame(json_data)

However i am getting the below error

flat_data = as.data.frame(json_data) Error in (function (..., row.names = NULL, check.rows = FALSE, check.names = TRUE, : arguments imply differing number of rows: 1, 13, 3201

Here is a sample of my JSON structure

{
    "RIDE":{
        "STARTTIME":"2020\/01\/05 22:27:49 UTC ",
        "RECINTSECS":1,
        "DEVICETYPE":"Garmin FR735XT ",
        "IDENTIFIER":" ",
        "TAGS":{
            "Aerobic Training Effect":"3.8 ",
            "Athlete":"Chuck Finley",
            "Data":" ",
            "Device":"Garmin",
            "Device Info":"HR Garmin 2327",
            "File Format":" ",
            "Filename":"2020_01_06_06_27_49.json ",
            "Month":"January ",
            "Performance Condition":"-5 ",
            "Recovery Time":"  ",
            "Source Filename":"A1662750_2020_01_06_06_27_49.gz ",
            "Sport":"Run ",
            "SubSport":" ",
            "VO2max detected":"61.7 ",
            "Weekday":"Mon ",
            "Workout Code":" ",
            "Year":"2020 "
        },
        "INTERVALS":[
            { "NAME":"Lap 1 ", "START": 0, "STOP": 249, "COLOR":"#000000", "PTEST":"false" },
            { "NAME":"Lap 2 ", "START": 250, "STOP": 504, "COLOR":"#000000", "PTEST":"false" }
        ],
        "SAMPLES":[
            { "SECS":0, "KM":0, "KPH":0, "HR":104, "ALT":14, "LAT":-40.402758436, "LON":175.0371112, "SLOPE":0, "LRBALANCE":0, "RCAD":109.5, "RVERT":0.47, "RCON":273 },
            { "SECS":1, "KM":0.00056, "KPH":0, "HR":104, "ALT":14, "LAT":-40.402758436, "LON":175.0371112, "SLOPE":0, "LRBALANCE":24.87, "RCAD":109.5, "RVERT":0.47, "RCON":273 }
        ],
        "XDATA":[
        {
            "NAME" : "EXTRA",
            "VALUES" : [ "STANCETIMEPERCENT", "VERTICALRATIO", "STEPLENGTH", "FIELD_88", "ACTIVITYTYPE", "PERFORMANCECONDITION" ],
            "UNITS" : [ "", "", "", "", "", "" ],
            "SAMPLES" : [
                { "SECS":1, "KM":0, "VALUES":[ 48.5, 0, 0, 300, 1, 0 ] },
                { "SECS":2, "KM":0, "VALUES":[ 48.5, 4.96, 891, 300, 1, 0 ] }
            ]
        }
        ]
    }
}

I am quite new to R, so any advise would be appreciated.


回答1:


Just change the file name. Json file should be .json

json_file <- "json file.json"


来源:https://stackoverflow.com/questions/59853523/flatten-nested-json-to-dataframe-in-r

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!