Reputation: 76
I have below json data I need to replace "=" to ":" by Javascript
{ "name"="John", "age"=30, "car"=null }
Expected output:
{ "name":"John", "age":30, "car":null }
Upvotes: 0
Views: 7940
Reputation: 37755
You can use Replace
let op = `{ "name"="John", "age"=30, "car"=null }`.replace(/=/g, ':')
console.log(op)
Upvotes: 0
Reputation: 2086
Use g
flag:
'{ "name"="John", "age"=30, "car"=null }'.replace(/\=/g, ':')
Upvotes: 0
Reputation: 4967
This should do the trick:
var str = '{ "name"="John", "age"=30, "car"=null }';
str = str.replace(/=/g,":");
var json = JSON.parse(str);
Note, that it would convert ALL "=" to ":". If there can be symbol in name or value, different approach should be used.
-- Update "g" modifier has to be used if there is more than one "=" to replace.
Upvotes: 2