将字符串转换为json字符串并在R

时间:2018-07-02 20:53:21

标签: r

我有一个列为json字符串的数据:

reservation  reasons
1592         [{"name"=>"jorge", "value"=>"MX"}, {"name"=>"Billing phone number", "value"=>"1123"}, {"name"=>"BillingCountry", "value"=>"USA"}]
1597         [{"name"=>"BillingAddress_Country", "value"=>"IN"}, {"name"=>"Billing phone number country code", "value"=>"IN"}, {"name"=>"Latest amount", "value"=>"583000000"}]

我想按如下方式分析该列:

reservation   name                                value
1592          jorge                                mx
1592          Billing phone number                 1123
1592          BillingCountry                       USA
1597          BillingAddress_Country               IN
1597          Billing phone number country code    IN
1597          Latest amount                        583000000

我是我们 R中的jsonlite。我的代码中出现以下错误:

data<-read.csv("data.csv")
json<-data$reasons
mydf <- fromJSON(json)
Error: Argument 'txt' must be a JSON string, URL or file.

谁能告诉我我在哪里犯错误?我需要做哪些修改?提前非常感谢!

2 个答案:

答案 0 :(得分:1)

对我来说,这看起来不像是普通的JSON(或者对fromJSON来说,这似乎使我感觉更好)。也许是某种特殊情况或其他原因(?)。 更新: @camille将其标识为Ruby Hash。

无论如何,我们都可以修复它:

reasons <-  '{"name"=>"jorge", "value"=>"MX"}, {"name"=>"Billing phone number", "value"=>"1123"}, {"name"=>"BillingCountry", "value"=>"USA"}'

reasons <- gsub("=>", ":", reasons)
reasons <- gsub("[{}]", "", reasons)
reasons <- paste0("{",reasons,"}")

fromJSON(reasons)
$`name`
[1] "jorge"

$value
[1] "MX"

$name
[1] "Billing phone number"

$value
[1] "1123"

$name
[1] "BillingCountry"

$value
[1] "USA"

答案 1 :(得分:1)

  dat%>%
  mutate(reasons=str_split(gsub("[^=A-Za-z,0-9{} ]+","",reasons),"(?<=\\}),\\s*"))%>%
  unnest()%>%
  mutate(names=str_extract(reasons,"(?<=name=)[^,}]+"),
         values=str_extract(reasons,"(?<=value=)[^,}]+"),
         reasons=NULL)

 reservation                             names    values
1        1592                             jorge        MX
2        1592              Billing phone number      1123
3        1592                    BillingCountry       USA
4        1597             BillingAddressCountry        IN
5        1597 Billing phone number country code        IN
6        1597                     Latest amount 583000000

使用此代码,如果您需要电子邮件,只需添加email=str_extract..