0

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.

Vine
  • 395
  • 3
  • 13
  • I'm afraid I don't get any error when I use your example. I do use "test_file.json", i.e. the name of the file containing JSON, instead of "json file", a string which `fromJSON` will try to interpret as JSON. `as.data.frame` works as expected and produces a data frame of the JSON data. –  Jan 22 '20 at 06:34
  • Hi @gersht, Thanks - I have tested using the data i provided and it worked, however that is only test data. Which has allowed me to narrow down the problem, In the real data set there is 13 records for "INTERVALS" and 3201 records for "Sample". Thanks! – Vine Jan 22 '20 at 07:19
  • I assume your question is solved? – Shahin Jan 22 '20 at 07:42

1 Answers1

0

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

json_file <- "json file.json"