0

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 }
2
  • Possible duplicate of JavaScript - Replace all commas in a string Commented Jan 12, 2019 at 13:47
  • That's not a valid json string. You can play around with replace but it's better to fix how that json is generated. Every language has a method to convert an object json string. Commented Jan 12, 2019 at 13:53

3 Answers 3

2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for your answer, But I am getting output :- {name: "John", age: 30, car: null}
0

Use g flag:

'{ "name"="John", "age"=30, "car"=null }'.replace(/\=/g, ':')

Comments

0

You can use Replace

let op = `{ "name"="John", "age"=30, "car"=null }`.replace(/=/g, ':')

console.log(op)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.