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

Posted

技术标签:

【中文标题】删除 JSON 数组中对象之间的逗号【英文标题】:Removing the commas between objects in a JSON array 【发布时间】: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
                            
                    
            

【问题讨论】:

Array of objects 需要逗号。问题可能是其他原因。 有类似的问题,但方向相反。看这里:***.com/questions/38695977/… 按照他们的documentation,您的预期输出似乎是正确的,但这不是Array of Objects 【参考方案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);

【讨论】:

是的,我可以做到。我正在寻找一些正则表达式来删除它。 类似***.com/questions/39655387/… 使用正则表达式很容易,前提是您的数据是“安全的”(我的意思是:如果您的数据不包含任何“”或“”)。【参考方案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);

【讨论】:

以上是关于删除 JSON 数组中对象之间的逗号的主要内容,如果未能解决你的问题,请参考以下文章

js中数组中元素之间的逗号如何去掉

您可以在 JSON 对象中使用尾随逗号吗?

删除 implode 之前或之后由数组引起的所有逗号

JSON对象与字符串相互转化ObjectMapper

C/C++,Java,PHP,JavaScript,Json数组对象赋值时,最后一个元素后面是否可以带逗号?

js中js数组对象与json之间的转换