I need a correct and easy way to convert a JSON String to object (javascript code string), like:
"'attribute': {
'attribute': 'value',
'attribute2': 0
}"
to
"attribute: {
attribute: 'value',
attribute2: 0
}"
The thing is remove the '
around the attribute.
The porpose of this is to help convert a object to a javascript code using the JSON.stringfy().
This regex can remove single quotes from around the property names. There will be some extreme cases that would not be working with this regex. But for simple objects as cited in your question, this is good.
var jsonstr = "{ 'attribute': 'value', 'attribute2': 0, 'parentattr': {'x': 0}} ";
jsonstr = jsonstr.replace(/'([^']+)':/g, '$1:');
console.log(jsonstr);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With