Removing the commas between objects in a JSON array Removing the commas between objects in a JSON array json json

Removing the commas between objects in a JSON array


You could:

  • json parse your code,
  • loop on rows
  • output each row as json stringified

As:

// Your Array as Stringlet myArray_as_string = `[  {    "a": "first", "objet": "with commas"  },  {    "an": "other", "objet": "2"  },  {    "a": "third", "objet": "3"  }]`;// JSON parse the string to get a JS Objectlet myArray_as_object = JSON.parse(myArray_as_string);// Make a string with each stringified rowlet myArray_without_commas = myArray_as_object.map( row => {    return JSON.stringify(row);  }).join("\n")// Do something with the result valueconsole.log(myArray_without_commas);


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


If you already have a string representation. Works as long as there is no JSON strings inside the JSON object.

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