在R中将json文件转换为CSV

时间:2016-04-18 14:50:48

标签: json r format-conversion

我已经从yelp下载了业务json文件,在其中进行了一些数据挖掘,但文件在json中,我希望它在csv中。该文件包含以下格式:

{
    '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),
        ...
    },
}

如何将其转换为csv?

2 个答案:

答案 0 :(得分:0)

你的意思是CSV吗? JSON非常方便解析,您可以使用R轻松地将其加载到数据框中,然后在需要时将其另存为CSV。

这个post很好地描述了使用R导入JSON的方法。完成后,您只需使用write.csv()重写CSV文件中的数据。以下是相关文档:https://stat.ethz.ch/R-manual/R-devel/library/utils/html/write.table.html

答案 1 :(得分:0)

包裹:
库(HTTR)
库(jsonlite)

图书馆(rlist)

我在将JSON转换为dataframe / CSV时遇到了问题。对于我的情况,我做了:

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)

然后从df到CSV。 在这种格式中,如果需要,应该很容易将其转换为多个.csv。

重要的部分是内容功能应该有type ='text'。

相关问题