【问题标题】:How to extract json data from csv file in R如何从R中的csv文件中提取json数据
【发布时间】:2018-06-23 10:33:00
【问题描述】:

我正在尝试从 R 中的 CSV 文件中提取 JSON 数据。我是 JSON 和 R 的新手,所以真的需要一些帮助。

我有一个 CSV 文件,它有 3 列 - 2 列是 namepublished_date。然而,第三列 ratings 包含 JSON 格式的数据。我正在尝试提取该数据,以便我拥有一个包含纯列的 CSV 文件(不再是 JSON 格式)。有人可以帮忙吗?

Data - 
**name** -> Test1         **published_date**-> 1151367060   **ratings** ->
[{'id': 7, 'name': 'Funny', 'count': 19645}, {'id': 1, 'name': 'Beautiful', 'count': 4573}, {'id': 9, 'name': 'Ingenious', 'count': 6073}, {'id': 3, 'name': 'Courageous', 'count': 3253}, {'id': 11, 'name': 'Longwinded', 'count': 387}, {'id': 2, 'name': 'Confusing', 'count': 242}, {'id': 8, 'name': 'Informative', 'count': 7346}, {'id': 22, 'name': 'Fascinating', 'count': 10581}, {'id': 21, 'name': 'Unconvincing', 'count': 300}, {'id': 24, 'name': 'Persuasive', 'count': 10704}, {'id': 23, 'name': 'Jaw-dropping', 'count': 4439}, {'id': 25, 'name': 'OK', 'count': 1174}, {'id': 26, 'name': 'Obnoxious', 'count': 209}, {'id': 10, 'name': 'Inspiring', 'count': 24924}]

【问题讨论】:

  • 你能在某处提供 csv 数据吗?例如gist?我认为单引号不是有效的 json,所以我不确定你是否提供了一个很好的例子
  • 使用rjson 包。你安装了吗?

标签: json r csv


【解决方案1】:

首先,这是解析 json 数据的方法

# if you read the data in a table with 3 column and 1 line
tab <- data.frame(name = "Test1",
           published_date = "1151367060",
           ratings ="[{'id': 7, 'name': 'Funny', 'count': 19645}, {'id': 1, 'name': 'Beautiful', 'count': 4573}, {'id': 9, 'name': 'Ingenious', 'count': 6073}, {'id': 3, 'name': 'Courageous', 'count': 3253}, {'id': 11, 'name': 'Longwinded', 'count': 387}, {'id': 2, 'name': 'Confusing', 'count': 242}, {'id': 8, 'name': 'Informative', 'count': 7346}, {'id': 22, 'name': 'Fascinating', 'count': 10581}, {'id': 21, 'name': 'Unconvincing', 'count': 300}, {'id': 24, 'name': 'Persuasive', 'count': 10704}, {'id': 23, 'name': 'Jaw-dropping', 'count': 4439}, {'id': 25, 'name': 'OK', 'count': 1174}, {'id': 26, 'name': 'Obnoxious', 'count': 209}, {'id': 10, 'name': 'Inspiring', 'count': 24924}]",
           stringsAsFactors = FALSE)

# Use jsonlite for parsing json
library(jsonlite)
# single quote is invalid, so if real, you need to replace them all by double quote
tab$ratings <- gsub("'", "\"", tab$ratings)
# parse the json
rating <- fromJSON(tab$ratings)
rating
#>    id         name count
#> 1   7        Funny 19645
#> 2   1    Beautiful  4573
#> 3   9    Ingenious  6073
#> 4   3   Courageous  3253
#> 5  11   Longwinded   387
#> 6   2    Confusing   242
#> 7   8  Informative  7346
#> 8  22  Fascinating 10581
#> 9  21 Unconvincing   300
#> 10 24   Persuasive 10704
#> 11 23 Jaw-dropping  4439
#> 12 25           OK  1174
#> 13 26    Obnoxious   209
#> 14 10    Inspiring 24924

您可以使用以下方法将此解析保留在输入表中 tidyverse 管道工作流和小标题。使用创建列表的能力 列,您可以将 fromJSON 结果存储在表中以代替 json 字符串

library(tidyverse)
tab %>%
  # convert to tibble for nice printing
  as_tibble() %>%
  # work on ratings column
  mutate(
    # replace single quote
    ratings = gsub("'", "\"", ratings),
    # create a list column with the result
    ratings= list(jsonlite::fromJSON(ratings))
  ) %>%
  # unnest the list column
  unnest()
#> # A tibble: 14 x 5
#>    name  published_date    id name1        count
#>    <chr> <chr>          <int> <chr>        <int>
#>  1 Test1 1151367060         7 Funny        19645
#>  2 Test1 1151367060         1 Beautiful     4573
#>  3 Test1 1151367060         9 Ingenious     6073
#>  4 Test1 1151367060         3 Courageous    3253
#>  5 Test1 1151367060        11 Longwinded     387
#>  6 Test1 1151367060         2 Confusing      242
#>  7 Test1 1151367060         8 Informative   7346
#>  8 Test1 1151367060        22 Fascinating  10581
#>  9 Test1 1151367060        21 Unconvincing   300
#> 10 Test1 1151367060        24 Persuasive   10704
#> 11 Test1 1151367060        23 Jaw-dropping  4439
#> 12 Test1 1151367060        25 OK            1174
#> 13 Test1 1151367060        26 Obnoxious      209
#> 14 Test1 1151367060        10 Inspiring    24924

reprex package (v0.1.1.9000) 于 2018 年 1 月 14 日创建。

【讨论】:

  • 谢谢@reprex 包。这真的很有帮助:) 如果我必须在 R 中读取包含多个相似数据行的 file.csv 怎么办?在这种情况下如何获得一个文件输出?
  • 你能举个例子吗?
猜你喜欢
  • 2018-05-20
  • 2021-10-25
  • 1970-01-01
  • 1970-01-01
  • 2013-08-07
  • 2020-03-08
  • 1970-01-01
  • 2019-06-15
  • 2021-06-24
相关资源
最近更新 更多