-1

In short I have this JSON configuration that looks like this:

{
    "Users" : {
        "182723618273612" : 15,
        "AddedUser" : 1
    }
}

I created the field through a JavaScript function, but Im trying to figure out how I can change the name of "AddedUser". I do NOT want t o change the value (1), I just want to change "AddedUser" to be another ID.

It should look like this:

{
    "Users" : {
        "182723618273612" : 15,
        "693582876127862" : 1
    }
}
Yochran
  • 17
  • 1
  • 4

1 Answers1

1

You can do the followig:

const users = JSON.parse(json);

const newKey = "693582876127862";

users[newKey] = users['AddedUser']; // with this, you can use variable as a key

delete users['AddedUser'];

JSON.stringify(users);

mehowthe
  • 761
  • 6
  • 14
  • 1
    This almost works, except `users` would be an object with a `users` property. --- Try `const { Users: users } = JSON.parse(json);` instead? – evolutionxbox Feb 12 '21 at 14:44