Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I convert Json to data frame in R

I'd like to convert my json data to data frame in R. Here is what I've done so far:

library("rjson")
result <- fromJSON(file ="mypath/data.json")
json_data_frame <- as.data.frame(result)

However, it comes to an error like this:

Error in data.frame(company_id = "12345678", country_name = "China", : arguments imply differing number of rows: 1, 2, 0

I also tried the following code:

library("rjson")
result <- fromJSON(file ="mypath/data.json")
final_data <- do.call(rbind, result)

And this error comes up:

Warning message: In (function (..., deparse.level = 1) : number of columns of result is not a multiple of vector length (arg 3)

I don't know what is going on here and how can I solve it. I will appreciate if I could get some help on this.

Here are some pieces of my json data:

{"business_id": "1234567", "Country_name": "China", "hours": {"Monday": {"close": "02:00", "open": "11:00"}, "Tuesday": {"close": "02:00", "open": "11:00"}, "Friday": {"close": "02:00", "open": "11:00"}, "Wednesday": {"close": "02:00", "open": "11:00"}, "Thursday": {"close": "02:00", "open": "11:00"}, "Sunday": {"close": "02:00", "open": "12:00"}, "Saturday": {"close": "02:00", "open": "12:00"}}, "open": true, "categories": ["Bars", "Nightlife", "Restaurants"], "city": "Beijing", "review_count": 5, "name": "Chen's Bar", "neighborhoods": ["West End"], "attributes": {"Take-out": true, "Wi-Fi": "free", "Good For": {"dessert": false, "latenight": false, "lunch": false, "dinner": false, "breakfast": false, "brunch": false}, "Good For Dancing": false, "Noise Level": "loud", "Takes Reservations": false, "Delivery": false, "Ambience": {"romantic": false, "intimate": false, "classy": false, "hipster": false, "divey": false, "touristy": false, "trendy": false, "upscale": false, "casual": false}, "Happy Hour": true, "Parking": {"garage": false, "street": false, "validated": false, "lot": false, "valet": false}, "Has TV": true, "Outdoor Seating": false, "Attire": "casual", "Alcohol": "full_bar", "Waiter Service": true, "Accepts Credit Cards": true, "Good for Kids": false, "Good For Groups": true, "Caters": true, "Price Range": 1}, "type": "business"}

like image 576
Ping Yi Hsu Avatar asked Apr 06 '16 14:04

Ping Yi Hsu


People also ask

How do I convert a JSON to a DataFrame?

Read JSON File into DataFrame You can convert JSON to Pandas DataFrame by simply using read_json() . Just pass JSON string to the function. It takes multiple parameters, for our case I am using orient that specifies the format of JSON string. This function is also used to read JSON files into pandas DataFrame.

Can you read JSON files in R?

R can read JSON files using the rjson package.

Can we convert JSON to table?

Yes, ImportJSON is a really easy tool to use for taking information from JSON and putting it into a table or spreadsheet. Including if you want to parse your JSON directly from Google Sheets!

How to convert JSON to Dataframe in pandas?

Pandas read_json () function is a quick and convenient way for converting simple flattened JSON into a Pandas DataFrame. When dealing with nested JSON, we can use the Pandas built-in json_normalize () function.

How to deal with JSON files in R language?

JavaScript Object Notation (JSON) stores text-based data in a human-readable format. It helps to transfer data between multiple web applications, from server side to client for display, etc., and vice versa. To deal with JSON files in R language, the rjson package is required. You can install the rjson package through the following command:

What is rjson package in Java?

JavaScript Object Notation (JSON) stores text-based data in a human-readable format. It helps to transfer data between multiple web applications, from server side to client for display, etc., and vice versa. To deal with JSON files in R language, the rjson package is required.

How to convert a list to a JSON object?

As @mishabalyasin suggested, jsonlite is a well-rounded package that can convert both to and from JSON. We'll convert the above object your_list to a JSON object, and then coerce it back into a list, this is done with jsonlite::toJSON () and jsonlite::fromJSON ().


2 Answers

Try using jsonlite library. It work for me

fromJSON(temp) %>% as.data.frame

Following is output enter image description here

if you want list.

fromJSON(temp) 
like image 84
Kush Patel Avatar answered Oct 05 '22 05:10

Kush Patel


Load the jsonlite package

library(jsonlite)

wine_json is a JSON

wine_json <- '{"name":"Chateau Migraine", "year":1997, "alcohol_pct":12.4, "color":"red", "awarded":false}'

Convert wine_json into a list:

wine <- fromJSON(wine_json)

Print structure of wine

str(wine)
like image 27
Adarsh Pawar Avatar answered Oct 05 '22 07:10

Adarsh Pawar