I am getting a json object like this
{
"First": "MyName's",
"Last": "MyLast"
}
I want to stringify this object so that 's in value become \'
it could be 's or 'S or 'anything
I am using JSON.stringify(json_obj) but its giving me string
"{"First":"MyName's","Last":"MyLast"}"
you can see MyName's I want this to MyName\'s
Try using a regex replace incase if you going to have more than once such values,
.stringify(data).replace(/'/g, "\\'")
DEMO: http://jsfiddle.net/qMsyg/2/
after you've stringified the json
just apply a replace("'", "\'");
JSON.stringify(json).replace("'", "\'");
Or you may use a replacer parameter into stringify()
method
JSON.stringify(json, function(key, value) {
return value.replace("'", "\'");
})
NOTE: replace("'", "\'")
will only replace the first occurrence, as pointed out by @vega. If you have more values to escape use a regular expression (like replace(/'/g, "\\'")
).
Choose the one which best fits your needs
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