为了账号安全,请及时绑定邮箱和手机立即绑定

有没有办法将此数据格式转换为 CSV?

有没有办法将此数据格式转换为 CSV?

ibeautiful 2022-05-24 10:43:28
我有大量带有格式的提取 Json 文件。我想知道是否有任何方法可以将其转换为以列作为特征和行中的值的 CSV。{"state": "New Jersey", "text": "RT @joncoopertweets: Register to join the #WeThePeopleMarch on September 21st in Washington, D.C. \u2014 or one of the 50+ marches that will be\u2026", "has_emoji": false, "created_at": "Mon Sep 02 16:32:05 +0000 2019", "id": 1168562246349467649, "entities": {"hashtags": [{"text": "WeThePeopleMarch", "indices": [42, 59]}], "urls": [], "user_mentions": [{"screen_name": "joncoopertweets", "name": "Jon Cooper", "id": 27493883, "id_str": "27493883", "indices": [3, 19]}], "symbols": []}, "source": "Twitter for iPad", "location": "Leonia, NJ", "verified": false, "geocode": null}{"state": "Indiana", "text": "RT @dariusherron1: Don\u2019t nobody love they girl like Mexicans ", "has_emoji": false, "created_at": "Mon Sep 02 16:32:05 +0000 2019", "id": 1168562246378827776, "entities": {"hashtags": [], "urls": [{"url": "", "expanded_url": "", "display_url": "", "indices": [61, 84]}], "user_mentions": [{"screen_name": "dariusherron1", "name": "Darius Herron", "id": 1680891876, "id_str": "1680891876", "indices": [3, 17]}], "symbols": []}, "source": "Twitter for iPhone", "location": "Indianapolis, IN", "verified": false, "geocode": null}
查看完整描述

2 回答

?
跃然一笑

TA贡献1826条经验 获得超6个赞

我对您的预期输出并不完全清楚(请参阅@user5783745 答案的评论和讨论)。您的 JSON 字符串包含一些嵌套对象,list如果您使用jsonlite::fromJSON. 由于您没有为您提供的示例数据提供匹配的预期输出,因此可能有不同的方法来处理这些嵌套条目。


一种可能性是解析 JSON 字符串,然后在绑定行之前解析两次flatten结果。list


library(tidyverse)

library(jsonlite)

map(json, ~fromJSON(.x) %>% flatten() %>% flatten()) %>% bind_rows()

## A tibble: 2 x 15

#  state text  has_emoji created_at     id indices screen_name name  id_str

#  <chr> <chr> <lgl>     <chr>       <dbl> <list>  <chr>       <chr> <chr>

#1 New … WeTh… FALSE     Mon Sep 0… 2.75e7 <int [… joncoopert… Jon … 27493…

#2 Indi… "RT … FALSE     Mon Sep 0… 1.68e9 <int [… dariusherr… Dari… 16808…

## … with 6 more variables: source <chr>, location <chr>, verified <lgl>,

##   url <chr>, expanded_url <chr>, display_url <chr>

结果对象是一个tibble带有一些list列的对象。要存储为 CSV,您可以排除这些list列。


样本数据

json <- c(

    '{"state": "New Jersey", "text": "RT @joncoopertweets: Register to join the #WeThePeopleMarch on September 21st in Washington, D.C. \u2014 or one of the 50+ marches that will be\u2026", "has_emoji": false, "created_at": "Mon Sep 02 16:32:05 +0000 2019", "id": 1168562246349467649, "entities": {"hashtags": [{"text": "WeThePeopleMarch", "indices": [42, 59]}], "urls": [], "user_mentions": [{"screen_name": "joncoopertweets", "name": "Jon Cooper", "id": 27493883, "id_str": "27493883", "indices": [3, 19]}], "symbols": []}, "source": "Twitter for iPad", "location": "Leonia, NJ", "verified": false, "geocode": null}',

    '{"state": "Indiana", "text": "RT @dariusherron1: Don\u2019t nobody love they girl like Mexicans ", "has_emoji": false, "created_at": "Mon Sep 02 16:32:05 +0000 2019", "id": 1168562246378827776, "entities": {"hashtags": [], "urls": [{"url": "", "expanded_url": "", "display_url": "", "indices": [61, 84]}], "user_mentions": [{"screen_name": "dariusherron1", "name": "Darius Herron", "id": 1680891876, "id_str": "1680891876", "indices": [3, 17]}], "symbols": []}, "source": "Twitter for iPhone", "location": "Indianapolis, IN", "verified": false, "geocode": null}')



查看完整回答
反对 回复 2022-05-24
?
一只斗牛犬

TA贡献1784条经验 获得超2个赞

您可以轻松地将其转换为更易于使用 (a list) 的数据格式,但此后如何使用它取决于您自己。在这种情况下,数据列表不会自动变成 a data.frame- 您必须考虑如何转换它(假设某些列表项是单个项,而其他列表项本身就是data.frames


a <- '{"state": "New Jersey", "text": "RT @joncoopertweets: Register to join the #WeThePeopleMarch on September 21st in Washington, D.C. \u2014 or one of the 50+ marches that will be\u2026", "has_emoji": false, "created_at": "Mon Sep 02 16:32:05 +0000 2019", "id": 1168562246349467649, "entities": {"hashtags": [{"text": "WeThePeopleMarch", "indices": [42, 59]}], "urls": [], "user_mentions": [{"screen_name": "joncoopertweets", "name": "Jon Cooper", "id": 27493883, "id_str": "27493883", "indices": [3, 19]}], "symbols": []}, "source": "Twitter for iPad", "location": "Leonia, NJ", "verified": false, "geocode": null}' 


library(jsonlite)

library(dplyr)

a <- a %>% fromJSON 


new_dataframe <- data.frame(state=character(), 

                                text=character(), 

                                has_emoji=character(), 

                                id=character(), 

                                entities=character(), stringsAsFactors = FALSE)



new_dataframe[1, ] <- c(a$state, a$text, a$has_emoji, a$created_at, a$id) 


查看完整回答
反对 回复 2022-05-24
  • 2 回答
  • 0 关注
  • 87 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信