javascript - 删除 JSON 数组中对象之间的逗号

标签 javascript json amazon-redshift

我正在努力将 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/

相关文章:

javascript - 动画无法使用不同的 HTML 标记

php - 将json数据插入列表中的html页面

java - HTTP 状态 406。Spring MVC 4.0、jQuery、JSON

mysql - 将 RDS (MySQL) 架构自动复制到 AWS Redshift 的最佳方法是什么?

Javascript多维数组长度问题

javascript - 是否可以重命名 Firebase 实时数据库中的 key ?

javascript - JSLint 错误 "Unexpected Var"

javascript - 将 JSON 解析为自定义 'tree-like' 文件结构

database - 无法将 csv 导入 postgres(在位置 4194303 处发现意外字符)

go - SQL 查询在服务器上完成但程序永远不会恢复