Joe
Joe

Reputation: 183

JSON data to dataframe in R

I have a json file from which i am importing the data

myList = rjson::fromJSON(file = "JsData.json")
myList


[[1]]
[[1]]$key
[1] "type1|new york, ny|NYC|hit"
[[1]]$doc_count
[1] 12

[[2]]
[[2]]$key
[2] "type1|omaha, ne|Omaha|hit"
[[2]]$doc_count
[2] 8

But when I am trying to convert to a data frame by function below ,
do.call(rbind, lapply(myList, data.frame))

I am getting an error.-
Error in (function (..., row.names = NULL, check.rows = FALSE, check.names = TRUE, : arguments imply differing number of rows: 1, 0


I need to parse this data so that it can be used in excel csv. I looked at the solution for Getting imported json data into a data frame in R
but the output is not coming in the proper usable format in excel.

And the JsData.json sample data looks like this:

[{"key":"type1|new york, ny|NYC|hit","doc_count":12}, {"key":"type1|omaha, ne|Omaha|hit","doc_count":8}, {"key":"type2|yuba city, ca|Yuba|hit","doc_count":9}]

Upvotes: 0

Views: 1318

Answers (1)

tokiloutok
tokiloutok

Reputation: 467

You can try :

require(jsonlite)
 s  ='[{"key":"type1|new york, ny|NYC|hit","doc_count":12}, 
      .......
        "key":"type2|yuba city, ca|Yuba|hit","doc_count":9}]'

df <- fromJSON(s)
df

                             key doc_count
 1   type1|new york, ny|NYC|hit        12
 2    type1|omaha, ne|Omaha|hit         8
 3 type2|yuba city, ca|Yuba|hit         9

I don't know how you want to deal wiyh you key .....

Upvotes: 1

Related Questions