【问题标题】:Removing the commas between objects in a JSON array删除 JSON 数组中对象之间的逗号
【发布时间】:2017-10-25 09:21:17
【问题描述】:

我正在将 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
                            }
                    }
            }

【问题讨论】:

标签: javascript json amazon-redshift


【解决方案1】:

你可以:

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

【讨论】:

  • 是的,我可以做到。我正在寻找一些正则表达式来删除它。
  • 使用正则表达式很容易,前提是您的数据是“安全的”(我的意思是:如果您的数据不包含任何“{”或“}”)。
【解决方案2】:

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

【讨论】:

    猜你喜欢
    • 2017-08-12
    • 2019-05-10
    • 2015-09-05
    • 2016-07-06
    • 2019-02-12
    • 2018-05-30
    • 2019-01-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多