I need to remove the double quotes between the numbers in my json object previously stringify.
Here is the initial one:
[ { "coucou": "Tolu", "salut": "55", "tata": "NORMAL", "loulou": "Tolu", "tutu": "NORMAL", "toto": "Tolu", "Baba": "24" }
, { "coucou": "TRUE", "salut": "128", "tata": "LOW", "loulou": "Tolu", "tutu": "LOW", "toto": "TRUE", "Baba": "7" }
, { "coucou": "Salut", "salut": "15.7", "tata": "LOW", "loulou": "Toli", "tutu": "NORMAL", "toto": "toti", "Baba": "-5" }
, { "coucou": "Salut", "salut": "-148", "tata": "LOW", "loulou": "Toli", "tutu": "NORMAL", "toto": "toti", "Baba": "-578" }
]
The regex that I use for that is:
const regex2 = /"-?([0-9]+\.{0,1}[0-9]*)"/g
json = json.replace(regex2, '$1')
let json = `[ { "coucou": "Tolu", "salut": "55", "tata": "NORMAL", "loulou": "Tolu", "tutu": "NORMAL", "toto": "Tolu", "Baba": "24" }, { "coucou": "TRUE", "salut": "128", "tata": "LOW", "loulou": "Tolu", "tutu": "LOW", "toto": "TRUE", "Baba": "7" }, { "coucou": "Salut", "salut": "15.7", "tata": "LOW", "loulou": "Toli", "tutu": "NORMAL", "toto": "toti", "Baba": "-5" }, { "coucou": "Salut", "salut": "-148", "tata": "LOW", "loulou": "Toli", "tutu": "NORMAL", "toto": "toti", "Baba": "-578" } ]` const regex2 = /"-?([0-9]+\.{0,1}[0-9]*)"/g json = json.replace(regex2, '$1') console.log( json)
But the result I get is that is also removes the negative operators so it changes all the results I want for my application:
[ { "X0": "Tolu", "X1": 55, "X2": "NORMAL", "X3": "Tolu", "X4": "NORMAL", "X5": "Tolu", "X6": 24 }
, { "X0": "TRUE", "X1": 128, "X2": "LOW", "X3": "Tolu", "X4": "LOW", "X5": "TRUE", "X6": 7 }
, { "X0": "Salut", "X1": 15.7, "X2": "LOW", "X3": "Toli", "X4": "NORMAL", "X5": "toti", "X6": 5 }
, { "X0": "Salut", "X1": 148, "X2": "LOW", "X3": "Toli", "X4": "NORMAL", "X5": "toti", "X6": 578 }
]
Does anybody know how to conserve the operators while removing double quotations marks around numbers with a regex?
for array of objects you can use the below code.
pass the Object to this function, it will return the updated object with all the values preserved.
function updateJson(objects) {
for (var i = 0; i < objects.length; i++) {
var obj = objects[i];
for (var prop in obj) {
if (obj.hasOwnProperty(prop) && obj[prop] !== null && !isNaN(obj[prop])) {
obj[prop] = +obj[prop];
}
}
}
return obj;
}
Your original code should work with
const regex2 = /"(-?[0-9]+\.{0,1}[0-9]*)"/g
json = json.replace(regex2, '$1')
let json = `[ { "coucou": "Tolu", "salut": "55", "tata": "NORMAL", "loulou": "Tolu", "tutu": "NORMAL", "toto": "Tolu", "Baba": "24" }, { "coucou": "TRUE", "salut": "128", "tata": "LOW", "loulou": "Tolu", "tutu": "LOW", "toto": "TRUE", "Baba": "7" }, { "coucou": "Salut", "salut": "15.7", "tata": "LOW", "loulou": "Toli", "tutu": "NORMAL", "toto": "toti", "Baba": "-5" }, { "coucou": "Salut", "salut": "-148", "tata": "LOW", "loulou": "Toli", "tutu": "NORMAL", "toto": "toti", "Baba": "-578" } ]` const regex = /"(-|)([0-9]+(?:\.[0-9]+)?)"/g json = json.replace(regex, '$1$2') console.log(json)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.