1

I have downloaded the business json file from yelp to do some data mining in it but the file is in json and I want it to be in csv. The file contains the following format:

{
    'type': 'business',
    'business_id': (encrypted business id),
    'name': (business name),
    'neighborhoods': [(hood names)],
    'full_address': (localized address),
    'city': (city),
    'state': (state),
    'latitude': latitude,
    'longitude': longitude,
    'stars': (star rating, rounded to half-stars),
    'review_count': review count,
    'categories': [(localized category names)]
    'open': True / False (corresponds to closed, not business hours),
    'hours': {
        (day_of_week): {
            'open': (HH:MM),
            'close': (HH:MM)
        },
        ...
    },
    'attributes': {
        (attribute_name): (attribute_value),
        ...
    },
}

How to convert it to csv ?

epo3
  • 2,991
  • 2
  • 33
  • 60
z. salim
  • 19
  • 1
  • 3

2 Answers2

0

Do you mean CSV? JSON is quite convenient to parse, you can easily load it in a dataframe with R and then save it as CSV if you still need to.

This post describes pretty well the way to import JSON with R. Once done, you just have to rewrite the data in a CSV file using write.csv(). Here is the associated doc : https://stat.ethz.ch/R-manual/R-devel/library/utils/html/write.table.html

Community
  • 1
  • 1
Plems
  • 11
  • 1
  • 4
0

Packages:
library(httr)
library(jsonlite)

Library (rlist)

I have had issues converting JSON to dataframe/CSV. For my case I did:

Token <- "245432532532"
source <- "http://......."
header_type <- "applcation/json"
full_token <- paste0("Bearer ", Token)
response <- GET(n_source, add_headers(Authorization = full_token, Accept = h_type), timeout(120), verbose())
text_json <- content(response, type = 'text', encoding = "UTF-8")
jfile <- fromJSON( text_json)
df <- as.data.frame(jfile)

Then from df to CSV. In this format is should be easu to convert it to multiple .csvs if needed.

The important part is content function should have type = 'text'.

K.Dᴀᴠɪs
  • 9,945
  • 11
  • 33
  • 43
Aaron C
  • 301
  • 1
  • 8