0

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}]

Community
  • 1
  • 1
Joe
  • 183
  • 5
  • 16

1 Answers1

1

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 .....

tokiloutok
  • 467
  • 5
  • 14
  • I am reading it from a JSON file. The data which I have posted is a sample data of that file. Hence I am explicitly calling the fromJSON function else with your suggested method, I got the error
    argument "txt" is missing, with no default
    The key column has pipe delimited data which i will covert into different columns in excel
    – Joe Jul 28 '16 at 14:38
  • Could you update you example with the problematic part ? – tokiloutok Jul 28 '16 at 21:46
  • This was the exact one I was getting errors on.
    myList = rjson::fromJSON(file = JSON file path)
    do.call(rbind, lapply(myList, data.frame))
    This is giving the error mentioned in the above question
    – Joe Jul 29 '16 at 12:44