【问题标题】:constructing json in R with jsonlite - too many brackets用 jsonlite 在 R 中构造 json - 括号太多
【发布时间】:2019-10-25 13:06:33
【问题描述】:

不是 json 专家,但我需要我认为被称为“嵌套对象”的东西,而我得到的是我认为被称为“嵌套数组”的东西。换句话说,一些额外的括号。我正在尝试使用 R 中的 jsonlite 将数据帧转换为 json 数据。下面的可重现代码和结果。谁能指出我如何以正确的格式获取数据(作为嵌套对象的行)?

library(jsonlite)

testdat <- data.frame(locationColumn = c("US", "US"),
                      nameColumn = c("General Motors", "Walmart"), 
                      zipColumn = c(19890, 72712) )


jsl <- jsonlite::toJSON(
  list(
    config = list(
      item1 = list("country",
                   "city"),
      item2 = "true",
      item3 = "false",
      item4 = 3
    ),
    rows = split(testdat, 1:nrow(testdat))
  ), 
  auto_unbox = TRUE,
  pretty = TRUE,
  dataframe = "rows",
  simplifyDataFrame = TRUE
)

jsl

输出:

{
  "config": {
    "item1": [
      "country",
      "city"
    ],
    "item2": "true",
    "item3": "false",
    "item4": 3
  },
  "rows": {
    "1": [
      {
        "locationColumn": "US",
        "nameColumn": "General Motors",
        "zipColumn": 19890
      }
    ],
    "2": [
      {
        "locationColumn": "US",
        "nameColumn": "Walmart",
        "zipColumn": 72712
      }
    ]
  }
} 

我需要什么:(编辑:我给 json 增加了一些复杂性。我需要将括号保留在“config”中,但在“rows”中没有括号。

{
  "config": {
    "item1": [
      "country",
      "city"
    ],
    "item2": "true",
    "item3": "false",
    "item4": 3
  },
  "rows": {
    "1":
      {
        "locationColumn": "US",
        "nameColumn": "General Motors",
        "zipColumn": 19890
      },
    "2":
      {
        "locationColumn": "US",
        "nameColumn": "Walmart",
        "zipColumn": 72712
      }
  }
} 

【问题讨论】:

    标签: r json jsonlite


    【解决方案1】:

    这是一个可能的解决方案:

    library(jsonlite)
    
    testdat <- data.frame(locationColumn = c("US", "US"),
                          nameColumn = c("General Motors", "Walmart"), 
                          zipColumn = c(19890, 72712) )
    
    jsl <- jsonlite::toJSON(
      list(
        rows = split(testdat, 1:nrow(testdat))
      ), 
      auto_unbox = TRUE,
      pretty = TRUE,
      dataframe = "columns",  #change from rows (moves brackets from row level to value level)
      simplifyDataFrame = TRUE
    )
    
    #removed the backets if desired
    #jsl<-gsub("\\[|\\]", "", jsl)
    
    all.equal(testcase, fromJSON(jsl))
    testcase<-fromJSON('{
      "rows": {
        "1":{
             "locationColumn": "US",
             "nameColumn": "General Motors",
             "zipColumn": 19890
            },
        "2":{
             "locationColumn": "US",
             "nameColumn": "Walmart",
             "zipColumn": 72712
            }
      }
    }')
    
    all.equal(testcase, fromJSON(jsl))
    #[1] TRUE
    

    编辑这是一个批准的版本,它手动编辑列表列表以获得正确的格式。

    #create a list of the data
    top<-list(
      config = list(
        item1 = list("country",
                     "city"),
        item2 = "true",
        item3 = "false",
        item4 = 3
      ),
      rows = split(testdat, 1:nrow(testdat))
    )
    
    #edit the data frames store as part of rows
    #lapply - lapply loops will parse each column in each row to create a new list
    rows<-lapply(top$rows, function(x){
      tempdf<-x
      #collist<-lapply(names(tempdf), function(y){print(tempdf[ , y, drop=T])})
      collist<-lapply(names(tempdf), function(y){tempdf[, y, drop=T]})
      names(collist)<-names(tempdf)
      collist
    })
    
    #update the list with the list of list
    top$rows<-rows
    
    #make the JSON
    jsl <- jsonlite::toJSON(
      top, 
      auto_unbox = TRUE,
      pretty = TRUE,
      dataframe = "columns",
      simplifyDataFrame = TRUE
    )
    

    【讨论】:

    • 我的简单示例可能太简单了,我将编辑我原来的问题。问题是这个 JSON 实际上还有更多内容和一些需要保留的括号。因此,去除所有括号的解决方案对我不起作用,但是如果我可以将它集中在“行”项目上,那么它会起作用。
    • @mb158127,如果您使用dataframe = "columns" 而不是“rows”,这是否提供了可接受的解决方案?它仍然有额外的括号,但现在处于不同的级别。
    • 这会将括号移动到键值对中的“值”周围,如下所示:``` { "config": { "item1": [ "country", "city" ] , "item2": "true", "item3": "false", "item4": 3 }, "rows": { "1": { "locationColumn": ["US"], "nameColumn": ["通用汽车"], "zipColumn": [19890] }, "2": { "locationColumn": ["US"], "nameColumn": ["The Hartford"], "zipColumn": [6105] } } } ``` 虽然在 API 中也会导致数据错误,所以我需要使用这些括号。
    • 我目前正试图弄清楚如何删除在特定关键字/模式之后出现的所有模式。换句话说,如果我可以 gsub "" 为 "[" 和 "]" 但只有出现在模式 "rows:" 之后的事件:
    • 换句话说,如果我能得到这个只删除括号“ef”之后,保留那些在“ef”之前,那么我已经解决了原来的问题。 .gsub("\[|\]", "", "ab[ cd] ef [ gh ] ij ", )
    猜你喜欢
    • 1970-01-01
    • 2022-01-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-18
    相关资源
    最近更新 更多