Need help with reading JSON API

Hi All

With regards to recent COVID 19 tracking I was trying to connect R Studio with the raw data JSON API for India. I am producing my code below and it works well. I wanted to export the raw data in csv and it is doing it's work however it is not picking up the variable names and all the variables gets named as V1/V2/V3 etc. Please help me on what am I missing in this piece of work.

library(httr)
library(jsonlite)
library(rlist)
library(dplyr)
library(data.table)
jsonResponse <-GET("https://api.covid19india.org/raw_data.json")
http_type(jsonResponse)
http_error(jsonResponse)
jsonResponseText <- content(jsonResponse, as = "text")
jsonResponseText
jsonResponseParsed <- content(jsonResponse, as="parsed")
jsonResponseParsed
fromJSON(jsonResponseText)
modJson<-jsonResponseParsed$raw_data
modJson
modJson%>% bind_rows %>% select(dateannounced, detectedcity, detecteddistrict, detectedstate, estimatedonsetdate, gender, nationality, patientnumber, statecode, statepatientnumber, statuschangedate, currentstatus)
list.select(modJson,dateannounced, detectedcity, detecteddistrict, detectedstate, estimatedonsetdate, gender, nationality, patientnumber, statecode, statepatientnumber, statuschangedate, currentstatus)
modJson<-jsonResponseParsed$raw_data
modJson
list.stack(modJson)
rawdata <- data.frame(matrix(unlist(modJson), nrow=27891, byrow=T),stringsAsFactors=FALSE)
write.csv(rawdata,'D:\Projects\mjs.csv', row.names = FALSE)

Any help would be much appreciated.

your code rather confused me as it seemed that you achieved your goal and then threw away the results and started over...
This is whats left when I comment out what I think was extraneous

library(httr)
# library(jsonlite)
# library(rlist)
library(dplyr)
# library(data.table)
jsonResponse <-GET("https://api.covid19india.org/raw_data.json")
# http_type(jsonResponse)
# http_error(jsonResponse)
# jsonResponseText <- content(jsonResponse, as = "text")
# jsonResponseText
jsonResponseParsed <- content(jsonResponse, as="parsed")
jsonResponseParsed
# fromJSON(jsonResponseText)
modJson<-jsonResponseParsed$raw_data
modJson
rawdata<- modJson%>% bind_rows %>% select(dateannounced, detectedcity, detecteddistrict, detectedstate, estimatedonsetdate, gender, nationality, patientnumber, statecode, statepatientnumber, statuschangedate, currentstatus)
# list.select(modJson,dateannounced, detectedcity, detecteddistrict, detectedstate, estimatedonsetdate, gender, nationality, patientnumber, statecode, statepatientnumber, statuschangedate, currentstatus)
# modJson<-jsonResponseParsed$raw_data
# modJson
# list.stack(modJson)
# rawdata <- data.frame(matrix(unlist(modJson), nrow=27891, byrow=T),stringsAsFactors=FALSE)
write.csv(rawdata,'D:\Projects\mjs.csv', row.names = FALSE)

A big thanks [nirgrahamuk]

Would you be kind enough to explain what exactly was missing. What was the exact role of the lines you made as comment.

Solution works like a charm however it would be beneficial for a new comer like me to understand what went wrong and what was redundant.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.