I have a JSON response that is not properly formatted and has speical characters ing in and I want to clean it up by removing the special characters using string.replace(). For some reason it is not working.
Here is the JSON result
[{"User::newPassword":["Password could not be changed. The request can't be validated"]},[]]
and here is my expression.
resp.replace(::g, '');
But it doesn't seem to work. Any advice on this would be appreciated as there is nothing I can do for it on the backend.
I have a JSON response that is not properly formatted and has speical characters ing in and I want to clean it up by removing the special characters using string.replace(). For some reason it is not working.
Here is the JSON result
[{"User::newPassword":["Password could not be changed. The request can't be validated"]},[]]
and here is my expression.
resp.replace(::g, '');
But it doesn't seem to work. Any advice on this would be appreciated as there is nothing I can do for it on the backend.
Share Improve this question edited Aug 31, 2015 at 4:18 Tushar 87.3k21 gold badges163 silver badges181 bronze badges asked Aug 31, 2015 at 3:31 Bazinga777Bazinga777 5,30113 gold badges56 silver badges95 bronze badges1 Answer
Reset to default 5You cannot use replace()
on JSON.
If you're working with strings
::
should be in quotes if you want to replace first occurrence of it.
resp.replace('::', '');
If you want to replace all occurrences, use /
as delimiter of regex.
resp.replace(/::/g, '');
If you're working with JSON
- Convert the
JSON
to string usingJSON.stringify()
- Use
replace
on string - Convert
string
back toJSON
usingJSON.parse()
Using Object methods
You can also change the key
to remove ::
from it
var newKey = key.replace('::', ''); // Create new key by replacing the `::`
obj[newKey] = obj[key]; // Add new key and value in object
delete key; // Remove old key