【问题标题】:Edit a list of json files with the file name of the json files in R使用 R 中 json 文件的文件名编辑 json 文件列表
【发布时间】:2020-12-02 18:44:47
【问题描述】:

我正在使用 R。 我有一组这样的 json 文件:

file1.json 文件2.json 文件3.json

每个文件都有相同的结构,里面有几个事件是这样组织的

{
    "id": MEME1,
    "created_at": 55796,
    "text": "patatipatata",
    "Perso": {
        "id": "MEMEuk",      
},

{
    "id": MEME2,
    "created_at": 55795,
    "text": "lolme",
    "Perso": {
        "id": "MEMEfr",
        
}

我想用每个事件的初始文件的名称来编辑每个 json 文件,例如 file1.json 和其他文件

{
    "file_name": file1.json,
    "id": MEME,
    "created_at": 55796,
    "text": "patatipatata",
    "Perso": {
        "id": "MEMEuk",
        

},
{
    "file_name": file1.json,        
    "id": MEME2,
    "created_at": 55795,
    "text": "lolme",
    "Perso": {
        "id": "MEMEfr",
        
}

我不太熟悉 json 结构和带有 R 的 json 版本。 最终目标是合并和展平这组 json 文件,并为每个事件获取一个带有原始文件名称的向量。最后一部分对我来说没问题。

感谢您的帮助。

【问题讨论】:

  • 那些文件不是有效的 json 文件(MEME1 应该被引用,嵌入的字典没有被关闭,应该包含在列表括号中 [/])。修复它是否安全,或者您的文件是否真的无效并且需要保持这种状态?

标签: r json


【解决方案1】:

出于多种原因,这些文件是无效的 json 文件。我将修复错误,以便它们正确解析。

  • file1.json

    [{
        "id": "MEME1",
        "created_at": 55796,
        "text": "patatipatata",
        "Perso": {
            "id": "MEMEuk"
        }
    },
    {
        "id": "MEME2",
        "created_at": 55795,
        "text": "lolme",
        "Perso": {
            "id": "MEMEfr"
        }
    }]
    
  • file2.json(简体)

    [{
        "id": "MEME3",
        "created_at": 55796
    },
    {
        "id": "MEME2",
        "created_at": 55795
    }]
    

阅读它们:

files <- list.files(pattern = "\\.json$", full.names = TRUE)
files
# [1] "./file1.json" "./file2.json"
alldat <- lapply(setNames(nm = files), jsonlite::read_json)

str(alldat[[2]]) # files[2], aka 'file2.json'
# List of 2
#  $ :List of 2
#   ..$ id        : chr "MEME3"
#   ..$ created_at: int 55796
#  $ :List of 2
#   ..$ id        : chr "MEME2"
#   ..$ created_at: int 55795

插入文件名

alldat2 <- Map(function(nm, onedat) {
  lapply(onedat, function(x) {
    x$file_name <- nm
    x
  })
}, names(alldat), alldat)

str(alldat2[[2]])
# List of 2
#  $ :List of 3
#   ..$ id        : chr "MEME3"
#   ..$ created_at: int 55796
#   ..$ file_name : chr "./file2.json"
#  $ :List of 3
#   ..$ id        : chr "MEME2"
#   ..$ created_at: int 55795
#   ..$ file_name : chr "./file2.json"

写回文件

Map(function(onedat, fn) jsonlite::write_json(onedat, fn, pretty = TRUE, auto_unbox = TRUE),
    alldat2, files)
# $`./file1.json`
# NULL
# $`./file2.json`
# NULL

readLines(files[2])
#  [1] "["                                  
#  [2] "  {"                                
#  [3] "    \"id\": \"MEME3\","             
#  [4] "    \"created_at\": 55796,"         
#  [5] "    \"file_name\": \"./file2.json\""
#  [6] "  },"                               
#  [7] "  {"                                
#  [8] "    \"id\": \"MEME2\","             
#  [9] "    \"created_at\": 55795,"         
# [10] "    \"file_name\": \"./file2.json\""
# [11] "  }"                                
# [12] "]"                                  

虽然结构(和 json 可解析性)应该没问题,但可能无法保证它们看起来 精确 就像他们在读入之前所做的那样。

【讨论】:

  • 这对我有用,直到插入文件名和 alldat2 文件创建部分。然后json发生了变化并且不容易变平,但我设法做到了。谢谢 r2evans。
  • 如果够用,请accept it。谢谢。
猜你喜欢
  • 2018-04-19
  • 2017-08-01
  • 2021-09-04
  • 2015-02-27
  • 2021-07-11
  • 1970-01-01
  • 2012-08-21
  • 2021-10-09
  • 2021-03-23
相关资源
最近更新 更多