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 OverflowWhat 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.
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
Remove commas in numbers for JSON output
How to remove comma atfer last line in json file
Remove commas in numbers for JSON output
Remove comma formatting for number column type in the Properties pane
You need to find ,, after which there is no any new attribute, object or array.
New attribute could start either with quotes (" or ') or with any word-character (\w).
New object could start only with character {.
New array could start only with character [.
New attribute, object or array could be placed after a bunch of space-like symbols (\s).
So, the regex will be like this:
const regex = /\,(?!\s*?[\{\[\"\'\w])/g;
Use it like this:
// javascript
const json = input.replace(regex, ''); // remove all trailing commas (`input` variable holds the erroneous JSON)
const data = JSON.parse(json); // build a new JSON object based on correct string
Try the first regex.
Another approach is to find every ,, after which there is a closing bracket.
Closing brackets in this case are } and ].
Again, closing brackets might be placed after a bunch of space-like symbols (\s).
Hence the regexp:
const regex = /\,(?=\s*?[\}\]])/g;
Usage is the same.
Try the second regex.
Consider the Json input = [{"ITEM1":{"names":["nameA"]}},{"ITEM2":{"names":["nameB","nameC"]}},] without whitespaces. I suggest a simple way using substring.
input = input.substring(0, input.length-2);
input = input + "]";
There are couple of approaches to achieve this:
Using Client Side Rendering
- Using CSR you can manipulate how your column data is visualized. But you'll need to manually put the JSLink everywhere.
Use Text Column and do validation
You can use a text column instead of number and do a below column validation:
=ISNUMBER([ColumnName]+0)
I added a JSON for the column formatting of the number column. Try the code below:
{
"$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"
}
]
}
if you just want to remove the commas:
systemList[0].comments.join("");
if you want to add space in between values:
systemList[0].comments.join(" ");
if you want to add anything in between values:
systemList[0].comments.join("anything");
reference for using .join() function:
https://www.w3schools.com/jsref/jsref_join.asp
I would prefer using one of this two methods:
var text = ',,,TEST STATUS GREEN,,TEST STATUS GREEN,';
var result1 = text.replace(/,/g,'')
console.log(result1)
var result2 = text.split(',').join('')
console.log(result2)
Greetings :)