我正在努力将 JSON 数据加载到 Redshift 中,但要使其正常工作,必须删除对象之间的逗号。如果我删除逗号那么它就可以正常工作。
有人可以告诉我如何删除对象之间的逗号,以便我可以将其加载到 Redshift 中吗?
Redshift 复制命令仅允许将对象数组作为行加载。
目前我有这个 JSON:
[{
"id":"57e4d12e53a5a",
"body":"asdas",
"published":"Fri,
23 Sep 2016 06:52:30 +0000",
"type":"chat-message",
"actor":
{
"displayName":"beau",
"objectType":"person",
"image":
{
"url":"http://www.gravatar.com/avatar/205e460b479e2e5b48aec07710c08d50?s=80&d=mm&r=g",
"width":48,"height":48
}
}
},
{
"id":"57e4d51165d97",
"body":"jackiechanSADAS",
"published":"Fri, 23 Sep 2016 07:09:05 +0000",
"type":"chat-message",
"actor":
{
"displayName":"beau",
"objectType":"person",
"image":
{
"url":"http://www.gravatar.com/avatar/205e460b479e2e5b48aec07710c08d50?s=80&d=mm&r=g",
"width":48,
"height":48
}
}
},
{
"id":"asas",
"body":"peterting",
"published":"Fri, 23 Sep 2016 07:09:05 +0000",
"type":"chat-message",
"actor":
{
"displayName":"beau",
"objectType":"person",
"image":
{
"url":"http://www.gravatar.com/avatar/205e460b479e2e5b48aec07710c08d50?s=80&d=mm&r=g",
"width":48,
"height":48
}
}
}]
我需要将其转换为:
{
"id":"57e4d12e53a5a",
"body":"asdas",
"published":"Fri,
23 Sep 2016 06:52:30 +0000",
"type":"chat-message",
"actor":
{
"displayName":"beau",
"objectType":"person",
"image":
{
"url":"http://www.gravatar.com/avatar/205e460b479e2e5b48aec07710c08d50?s=80&d=mm&r=g",
"width":48,"height":48
}
}
}
{
"id":"57e4d51165d97",
"body":"jackiechanSADAS",
"published":"Fri, 23 Sep 2016 07:09:05 +0000",
"type":"chat-message",
"actor":
{
"displayName":"beau",
"objectType":"person",
"image":
{
"url":"http://www.gravatar.com/avatar/205e460b479e2e5b48aec07710c08d50?s=80&d=mm&r=g",
"width":48,
"height":48
}
}
}
{
"id":"asas",
"body":"peterting",
"published":"Fri, 23 Sep 2016 07:09:05 +0000",
"type":"chat-message",
"actor":
{
"displayName":"beau",
"objectType":"person",
"image":
{
"url":"http://www.gravatar.com/avatar/205e460b479e2e5b48aec07710c08d50?s=80&d=mm&r=g",
"width":48,
"height":48
}
}
}
最佳答案
你可以:
- json 解析您的代码,
- 在行上循环
- 将每一行输出为 json 字符串化
如:
// Your Array as String
let myArray_as_string = `
[
{
"a": "first", "objet": "with commas"
},
{
"an": "other", "objet": "2"
},
{
"a": "third", "objet": "3"
}
]
`;
// JSON parse the string to get a JS Object
let myArray_as_object = JSON.parse(myArray_as_string);
// Make a string with each stringified row
let myArray_without_commas = myArray_as_object.map( row => {
return JSON.stringify(row);
}).join("\n")
// Do something with the result value
console.log(myArray_without_commas);
关于javascript - 删除 JSON 数组中对象之间的逗号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46928588/