How about doing the test.replace with a negative lookahead - https://regex101.com/r/WtHcuO/2/:

var data = JSON.stringify([{"var1":"0","description":"this, has, commas"},{"var1":"1","description":"more, commas"}]);

var stripped = data.replace(/,(?!["{}[\]])/g, "");

console.log(stripped);

Or, if you want to preserve the commas, but escape them, you can replace with \\, instead of ""

var data = JSON.stringify([{"var1":"0","description":"this, has, commas"},{"var1":"1","description":"more, commas"}]);

var stripped = data.replace(/,(?!["{}[\]])/g, "\\,");

console.log(stripped);

Answer from combatc2 on Stack Overflow
Top answer
1 of 6
8

What makes this a non-trivial problem is that the JSON format does not care about whitespace that does not occur inside keys or data. Therefore,

{ "key": "data" }

is the same as

{ "key":
"data"
}

If you add the possibility of a "broken" JSON file, such as

{ "key":
"data", }

it becomes really difficult to properly parse the document with anything other than a JSON parser that knows how to relax the restrictions of the JSON format when parsing the data.

The Perl JSON module can do that, and also pretty-print the result:

$ cat file.json
{
    "fruit": "Apple",
}
$ perl -MJSON -e '@text=(<>);print to_json(from_json("@text", {relaxed=>1}), {pretty=>1})' file.json
{
   "fruit" : "Apple"
}

Here, we read in the whole text document into the array @text. We then decode this while relaxing the parsing (this enables the JSON document to have commas before } and ] and also to include # comments). We then immediately encode the resulting Perl data structure into JSON again and print it.

Another example:

$ cat file.json
{
    "fruit": "Apple",   # a comment
    "stuff": [1, 2, 3,],
}
$ perl -MJSON -e '@text=(<>);print to_json(from_json("@text", {relaxed=>1}), {pretty=>1})' file.json
{
   "fruit" : "Apple",
   "stuff" : [
      1,
      2,
      3
   ]
}

Without pretty printing:

$ perl -MJSON -e '@text=(<>);print to_json(from_json("@text", {relaxed=>1}))' file.json
{"fruit":"Apple","stuff":[1,2,3]}

(no newline at the end of the output)

For really large documents, you would want to use the module's incremental parsing capability and write a proper script for the conversion.

2 of 6
5

With the GNU implementation of sed:

sed -i.bak ':begin;$!N;s/,\n}/\n}/g;tbegin;P;D' FILE

sed -i.bak = creates a backup of the original file, then applies changes to the file

':begin;$!N;s/,\n}/\n}/g;tbegin;P;D' = anything ending with , followed by new line and }. Remove the , on the previous line

FILE = the file you want to make the change to

Discussions

Remove commas in numbers for JSON output
Is there a way to format a number without commas in the template used for JSON output? I don’t want to lose the commas in the app, just in the JSON file output. More on googlecloudcommunity.com
🌐 googlecloudcommunity.com
14
1
November 11, 2019
How to remove comma atfer last line in json file
Hello, I am writing json in text file and it is ending with comma. i ned to remove that lsat comma, how can i reov ethat check below image. i need like below i have lst row number in variable which i ma fatchin… More on forum.uipath.com
🌐 forum.uipath.com
5
0
August 4, 2023
Remove commas in numbers for JSON output
Is there a way to format a number without commas in the template used for JSON output? I don’t want to lose the commas in the app, just in the JSON file output. More on discuss.google.dev
🌐 discuss.google.dev
14
1
November 11, 2019
Remove comma formatting for number column type in the Properties pane
I have JSON code that will remove the commas from a Number column type (in screen cap named 'Data ID') in the list view. But whenever a user selects an item... More on techcommunity.microsoft.com
🌐 techcommunity.microsoft.com
2
0
August 26, 2024
🌐
Google Developer forums
googlecloudcommunity.com › appsheet › appsheet q&a
Re: Remove commas in numbers for JSON output - Google Cloud Community
December 22, 2021 - I would think that only the raw value would get passed on in such a case, is there some rationale why the comma remains? ... @JNeufeld @Marc_Dillon Actually, you shouldn’t be needing to do this because the number formatting is not carried to a JSON parameter’s value. The number is always a number and when you apply the CONCATENATE expression, than the numerical figure will be converted to text.
🌐
Google Cloud
googlecloudcommunity.com › appsheet › appsheet q&a
Remove commas in numbers for JSON output - AppSheet Q&A - Google Developer forums
November 11, 2019 - Is there a way to format a number without commas in the template used for JSON output? I don’t want to lose the commas in the app, just in the JSON file output.
🌐
UiPath Community
forum.uipath.com › help › studio
How to remove comma atfer last line in json file - Studio - UiPath Community Forum
August 4, 2023 - Hello, I am writing json in text file and it is ending with comma. i ned to remove that lsat comma, how can i reov ethat check below image. i need like below i have lst row number in variable which i ma fatching using System.IO.File.ReadAllLines(“Error - Formatted\P003324-384_3.json”).Length
🌐
Microsoft Power Platform Community
powerusers.microsoft.com › t5 › Building-Flows › How-to-remove-comma-from-a-json-string-within-double-quotes › td-p › 1809430
Forums | Microsoft Power Platform Community
October 10, 2022 - Quickly search for answers, join discussions, post questions, and work smarter in your business applications by joining the Microsoft Dynamics 365 Community.
🌐
Google
discuss.google.dev › appsheet › appsheet q&a
Remove commas in numbers for JSON output - #3 by LeventK - AppSheet Q&A - Google Developer forums
November 11, 2019 - Is there a way to format a number without commas in the template used for JSON output? I don’t want to lose the commas in the app, just in the JSON file output.
Find elsewhere
🌐
SharePoint Diary
sharepointdiary.com › sharepoint diary › sharepoint online › sharepoint online: number column without comma
SharePoint Online: Number Column without Comma - SharePoint Diary
January 8, 2023 - To display numbers without commas in SharePoint Online, you can use the format column option in the Column settings for a list or library. Navigate to your list, Select the little drop-down in the target column, Choose “Column Settings” ...
🌐
JetBrains
youtrack.jetbrains.com › issue › WEB-19143 › Remove-last-comma-in-json-or-js-arrays-object-automatically
Remove last comma in json or js arrays/object automatically
{{ (>_<) }} This version of your browser is not supported. Try upgrading to the latest stable version. Something went seriously wrong
🌐
SharePoint Stuff
sharepointstuff.com › home › sharepoint version › sharepoint online › how to remove commas from number columns in lists
How to remove commas from number columns in Lists - SharePoint Stuff
June 6, 2021 - It was only once it was imported into SharePoint that the commas appeared. Left-click on your number column · Under Column Settings > select Format this column · Ensure the Format Columns tab is selected, scroll down and press Advanced mode · Remove the text from the box and add the following JSON: { "$schema": "https://developer.microsoft.com/json-schemas/sp/v2/column-formatting.schema.json", "elmType": "div", "attributes": { "class": "=if(@currentField > 0,'', '')" }, "children": [ { "elmType": "span", "style": { "display": "inline-block" } }, { "elmType": "span", "txtContent": "@currentField" } ] } Press Save ·
🌐
OfflineTools
offlinetools.org › a › json-formatter › handling-trailing-commas-in-json-a-formatters-approach
How to Remove Trailing Commas From JSON | Offline Tools
JSON Formatter tool · If you are trying to remove a comma from a JSON object or array, the rule is simple: standard JSON does not allow a comma immediately before a closing } or ]. Delete that final comma, then validate the document again.
🌐
SPGuides
spguides.com › remove-thousand-separator-sharepoint
SharePoint Online Number Column without Comma
March 26, 2025 - 3. However, we need to remove the thousand separator from the number column. For that, turn off or disable the Use thousands separator toggle, and click on the Save button. 4. Once you have completed all your updates, you will get the number column values without a thousand separator. Look at the below image. This is the simplest way to remove a comma in SharePoint Online number field. Similarly, you can also work with the SharePoint list number without a thousand separator using JSON Format.
🌐
GitHub
gist.github.com › ozzi- › 4eefe81a224f4fbfaaf415010fd213ee
removes trailing commas in JSON strings · GitHub
removes trailing commas in JSON strings · Raw · JSON - Remove Trailing Comma · This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
🌐
YouTube
youtube.com › dritalconnect
How to remove comma from number from string in JavaScript - YouTube
How to remove comma from number from string in JavaScript
Published   May 30, 2022
Views   910