-1
{
    1:' {name} testing for 1 ',
    2:'{name} testing for 2 ',
    3:'{name} testing for 3 ',
    4:'{name} testing for 4 ',
    5:'{name} testing for 5 '
  }

suppose I have above json data in some file now, I want to import it to some other file and want to change all the occurrence of {name} to Joye . can anyone help me with the efficient way. As one way is we can get the data in some variable and convert it into string and then replace it, but I was looking if we can pass some parameter to json file and it could change or something similar.

trincot
  • 317,000
  • 35
  • 244
  • 286
Pooja Kushwah
  • 189
  • 2
  • 2
  • 13
  • If you have it in a file you already have it as a string, I think that the best solution is the one you talk about, just replace all occurrences in that primary string and save it in the other file, just parsing it would be more ineficient than all the replacement – Edoldin Aug 12 '22 at 08:20
  • @evolutionxbox What does string interpolation have to do with the question? – Behemoth Aug 12 '22 at 08:21
  • 1
    @Behemoth [this answer](https://stackoverflow.com/a/1408373/989920) – evolutionxbox Aug 12 '22 at 08:21
  • Ahh I see, valid. – Behemoth Aug 12 '22 at 08:24
  • 2
    That's not JSON data. The JSON tag: _"Do not use this tag for native JavaScript objects or JavaScript object literals. Before you ask a question, validate your JSON using a JSON validator such as JSONLint (https://jsonlint.com). "_ JSON requires double-quotes for keys and strings. – jabaa Aug 12 '22 at 09:04

1 Answers1

0

Assuming you want to convert your JSON into a JavaScript object you can use the classic Object.fromEntries() + Object.entries() + Array.map() approach after parsing it.

const data = {
  1: " {name} testing for 1 ",
  2: "{name} testing for 2 ",
  3: "{name} testing for 3 ",
  4: "{name} testing for 4 ",
  5: "{name} testing for 5 "
};

const result = Object.fromEntries(
  Object.entries(data).map(([key, value]) => ([
    key, value.replace("{name}", "Joyce")
  ]))
);

console.log(result)
Behemoth
  • 5,389
  • 4
  • 16
  • 40
  • First you have to parse JSON. – jabaa Aug 12 '22 at 09:06
  • Read the first and only sentence of my answer again please. – Behemoth Aug 12 '22 at 09:16
  • 1
    Yes, the sentence is wrong. You can't convert JSON to a JavaScript object using `Object.fromEntries()` + `Object.entries()` + `Array.map()`. You have to parse it first. – jabaa Aug 12 '22 at 09:17
  • Isn't it clear enough to say *Assuming you want to convert your JSON into a JavaScript object*? I updated my question mr. grammar man :) – Behemoth Aug 12 '22 at 09:24
  • 1
    Maybe it's a language problem. _Assuming you want X, you can Y" means, Y is the way to achieve X, but `Object.fromEntries()` + `Object.entries()` + `Array.map()` isn't a way to parse JSON. – jabaa Aug 12 '22 at 09:26
  • What I was trying to say was *Assuming you want to accept the fact that* ... *you can do this and that*... I my opinion it's understandable that you don't parse the JSON with `Object.entries()`. Are you happy now? – Behemoth Aug 12 '22 at 09:28