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

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

3 Comments

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 <br> argument "txt" is missing, with no default<br> The key column has pipe delimited data which i will covert into different columns in excel
Could you update you example with the problematic part ?
This was the exact one I was getting errors on. <br> myList = rjson::fromJSON(file = JSON file path) <br> do.call(rbind, lapply(myList, data.frame)) <br> This is giving the error mentioned in the above question

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.