我已经阅读了Create nested JSON from flat csv,但对我来说没有帮助。
我有一个用 Google Docs 创建的很大的电子表格,包含 11 行和 74 列(有些列没有被占用)。
我在Google Drive上创建了一个示例。当导出为 aCSV时,它看起来像这样:
id,name,email,phone,picture01,picture02,picture03,status
1,Alice,alice@gmail.com,2131232,"image01_01
[this is an image]",image01_02,image01_03,single
2,Bob,bob@gmail.com,2854839,image02_01,"image02_02
[description to image 2]",,married
3,Frank,frank@gmail.com,987987,image03_01,image03_02,,single
4,Shawn,shawn@gmail.com,,image04_01,,,single
现在我想要一个JSON结构,如下所示:
{
"persons": [
{
"type": "config.profile",
"id": "1",
"email": "alice@gmail.com",
"pictureId": "p01",
"statusId": "s01"
},
{
"type": "config.pictures",
"id": "p01",
"album": [
{
"image": "image01_01",
"description": "this is an image"
},
{
"image": "image_01_02",
"description": ""
},
{
"image": "image_01_03",
"description": ""
}
]
},
{
"type": "config.status",
"id": "s01",
"status": "single"
},
{
"type": "config.profile",
"id": "2",
"email": "bob@gmail.com",
"pictureId": "p02",
"statusId": "s02"
},
{
"type": "config.pictures",
"id": "p02",
"album": [
{
"image": "image02_01",
"description": ""
},
{
"image": "image_02_02",
"description": "description to image 2"
}
]
},
{
"type": "config.status",
"id": "s02",
"status": "married"
}
]
}
其他线路依此类推。
我的理论方法是CSV每行遍历文件(这里开始第一个问题:现在每一行等于一行,但有时是几行,因此我需要计算逗号?)。每行等于 的一个块config.profile,包括id、email、pictureId和statusId(后两个根据行号生成)。
然后为每一行config.pictures生成一个块,该块与id插入块中的config.profile块相同。这album是一个包含与图片一样多的元素的数组。
最后,每一行都有一个config.status块,该块与id中给出的块相同,并且具有相应状态的config.profile一个条目。status
我完全不知道如何创建嵌套和条件 JSON 文件。
CSV我刚刚到了将 转换为 valid的地步JSON,没有任何嵌套和附加信息,这些信息在 中没有直接给出CSV,例如type、pictureId、statusId等等。
任何帮助表示赞赏。如果用另一种脚本语言(如 )更容易对此进行编程ruby,我很乐意切换到那些)。
在有人认为这是家庭作业或诸如此类之前。它不是。我只想自动化一个原本非常烦人的复制和粘贴任务。