Need help with reading JSON API for hierarchical data

Hi,

This is one more question related to importing data from JSON API.

library(httr)
library(dplyr)
jsonResponse <-GET("https://api.covid19india.org/districts_daily.json")
jsonResponseParsed dist<- content(jsonResponse, as="parsed")
jsonResponseParseddist
modJsondist<-jsonResponseParseddist$districtsDaily
modJsondist

This gives me a heirarchical data. From here my intention is to convert it in a readable format and export to csv file for further analysis. I use the below code:-

rawddist <- modJsondist%>% bind_rows %>% select("Andhra pradesh", "Bihar", "Delhi", "Gujarat", "Haryana", "Jammu and Kashmir", "Jharkhand", "Karnataka", "Kerala", "Ladakh", "Madhya Pradesh", "Maharashtra", "Odisha", "Punjab", "Rajasthan", "Tamil Nadu", "Telangana", "Uttar Pradesh", "West Bengal")

However it throws an error as shown below:-

Error: Argument 3 must be length 14, not 4

I am not sure what is going wrong.

I need two help here:-

  1. Some explannation on how to read the hierarchical data the JSON API returns
  2. Any help in terms of what is wrong with the code and how to fix it.

Not all elements in your list have the same length, and also the elements of the list are not data.frame. So bind_row alone won't be enough.

You need to use tools to get the content of your list structure into a table structure.
See for example about rectangling with tidyr
https://tidyr.tidyverse.org/articles/rectangle.html

There is tools to get json tidy

Hope it helps

It is not the best to learn if I do it in your place. There are some examples on the links I provided. Did you try something already ?

If you really want this data into CSV without doing the work of rectangling, it seems you can get the latest data in csv format directly in the API: https://api.covid19india.org/#csv, https://api.covid19india.org/csv/

You may have already :eyes: this though...

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

Hi,

This is something very new to me and hence I am feeling I am not being able to make it work. Would you mind helping me more on this. This is from Covid pandemic database for India and is purely for academic and learning purpose.