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

时间:2017-10-25 09:21:18

标签: javascript json amazon-redshift

我正在努力将JSON数据加载到Redshift中,但为了使其工作,必须在对象之间删除逗号。如果我删除逗号,那么它可以正常工作。

有人可以告诉我如何删除对象之间的逗号,以便将其加载到Redshift中吗?

Redshift copy命令只允许将对象数组作为行加载。

目前我有这个JSON:

Null

我需要将其转换为:

[{
        "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
                    }
            }
    }]

2 个答案:

答案 0 :(得分:2)

你可以:

  • json解析你的代码,
  • 循环行
  • 将每一行输出为json stringified

如:



// 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);




答案 1 :(得分:1)

let data = [{
        "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
                    }
            }
    }]
    
    output = data.map(function(e){return JSON.stringify(e,null,2)}).join("\n")
    console.log(output);

如果您已有字符串表示。只要JSON对象中没有JSON字符串,就可以正常工作。

let data = JSON.stringify([{
        "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
                    }
            }
    }],null,2);
    
    output = data.replace(/^\[/,"").replace(/]$/,"").replace(/}\,[\s]+{/g,"}\n\n{")
    console.log(output);