First of all, a word of warning: you should never provide authentication information in code you share as this might be sensitive information that could lead to issues if it falls into the wrong hands
I know you did this to help us recreate the issue, but next time try to see if you can generate the same issue without having to share authentication credentials. A very nice start guide is here
To the question at hand now:
JSON is not the same data structure as a table and can have nested lists of data which are not possible in csv. Sometimes you can easily convert if JSON does not have nested data, but in your case it is. Look at the "result.mailingAddress", "result.regionalbereich", ... they are nested data frames within a data frame. So you cannot simply convert this to a regular data frame. You can extract parts or remove them, but flattening everything into one giant table will be impossible or even if possible, will generate a massive table with a lot of repeating values.
Best way is to save the json as a json Then you can load it back whenever needed.
h <- new_handle()
handle_setheaders(h,Authorization = "Bearer 60ff659020cbdccfb2b7aacaa03af4f7")
url <- "https://api.deutschebahn.com/stada/v2/stations"
res <- curl_fetch_memory(url,h)
myjsondata <- rawToChar(res$content)
myjsondata <- fromJSON(prettify(myjsondata))
#Write the json
write_json(myjsondata, "allStationTODAYNOV.json", pretty = T)
#Read the json
test = read_json("allStationTODAYNOV.json", simplifyVector = T)
pretty = T makes sure the JSON is saved in a pretty more readable multi-line format with indentation instead of just one giant row of text
simplifyVector = T will try and simplify JSON files when read in by simplifying nested lists into vectors and data frames
the R dataframe, has tabular outer dimensions, but with nesting it supports entries which are themselves dataframes.
simple tabular formats such as csv, do not support this.
Yet, you can save the data.frame - the same as any R object though.
base::saveRDS accomplishes this.
(nested_tbl <- tibble(
a = c(1, 2),
b = list(3, tibble(x = 4, y = 5))
how would you manually compose a csv so that another program can read it with the same structure ?
remembering that csv is primitive and therefore you can only use commas and line breaks....