Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

169
Views
Replace a variable in JSON object

I'm writing JavaScript code in which I want to replace a string in JSON object and my code is as below.

var obj = {
  "name": "name is $name",
  "work": "$name is doctor",
  "maritial status": "unmarried"
};

obj = Object.keys(obj).map(function(key, index) {
  return (obj[key].includes('$name')) ? obj[key].replace('$name', 'John'): obj[key];
});

console.log(obj);

Here I want to replace $name with John and print the JSON, but unfortunately, this is printing only the values like below instead of the whole JSON.

["name is John", "John is doctor", "unmarried"]

Where I am going wrong and how can I fix this?

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

You keep mentioning JSON but what you have is a JavaScript object. JSON is a string. So it would be much easier for you to keep it as a string, do a simple replace on the name, and then parse it to an object.

const json = '{"name":"name is $name","work":"$name is doctor", "maritial status":"unmarried"}';

const updated = json.replaceAll('$name', 'John');

console.log(JSON.parse(updated));

about 4 years ago · Juan Pablo Isaza Report

0

If you want to get a transformed object as the result, either assign the changed value to the original object:

var obj = {
  "name": "name is $name",
  "work": "$name is doctor",
  "maritial status": "unmarried"
};

Object.keys(obj).forEach(function(key) {
  if (obj[key].includes('$name')) {
    obj[key] = obj[key].replace('$name', 'John');
  }
});

console.log(obj);

Or use Object.entries and Object.fromEntries to map to a new object:

var obj = {
  "name": "name is $name",
  "work": "$name is doctor",
  "maritial status": "unmarried"
};
const newObj = Object.fromEntries(
  Object.entries(obj).map(([key, val]) => [key, val.replace('$name', 'John')])
);

console.log(newObj);

about 4 years ago · Juan Pablo Isaza Report

0

You can use Object.entries method or other object methods to solve your problem one of them might be ...

let obj = {
  "name": "name is $name",
  "work": "$name is doctor",
  "maritial status": "unmarried"
};

Object.entries(obj).forEach(item => {
  if ((obj[item[0]].includes('$name'))) {
    item[1] = item[1].replace("$name", "John");
    obj[item[0]] = item[1];
  }
})
console.log(obj);

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!