0👍
You can trim both key and value using the following function:
function trimObj(obj) {
if (!Array.isArray(obj) && typeof obj != 'object') return obj;
return Object.keys(obj).reduce(function(acc, key) {
acc[key.trim()] = typeof obj[key] == 'string'? obj[key].trim() : trimObj(obj[key]);
return acc;
}, Array.isArray(obj)? []:{});
}
var a = '{ "name":"John ", "age":30, "city":" New York"}';
console.log(trimObj(JSON.parse(a)));
Or alternatively, You can stringify it, string replace, and reparse it
JSON.parse(JSON.stringify(badJson).replace(/"\s+|\s+"/g,'"'))
- [Vuejs]-Vue-cli-plugin-prerender-spa generate with error not found inside html file
- [Vuejs]-Sort django rest framework paginated list by date in vue
Source:stackexchange.com