H
H
hatealleverything2020-10-17 12:15:29
JavaScript
hatealleverything, 2020-10-17 12:15:29

How can I change keys in json object?

"57939553_675562008": {
"price": "5.00",
"market_hash_name": "Beast-Jaw Bludgeoner",
"ru_rarity": "Common"
}
I would like to change this name (market_hash_name), how can I do that?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexander, 2020-10-17
@Aleksandr-JS-Developer

Parse the JSON string into a regular object, change, "pack", as mentioned above, back.
Here are the docs: JSON Format (RU)

L
Loli E1ON, 2020-10-17
@E1ON

const json = `{"57939553_675562008": {
"price": "5.00",
"market_hash_name": "Beast-Jaw Bludgeoner",
"ru_rarity": "Common"
}}`;

const data = JSON.parse(json);

data['57939553_675562008'].custom_hash_name = data['57939553_675562008'].market_hash_name;
delete data['57939553_675562008'].market_hash_name;

const result = JSON.stringify(data);

console.log(result);

const data = JSON.parse(json);

delete Object.assign(data, {
  '57939553_675562008': {
    new_name: data['57939553_675562008'].market_hash_name,
    ...data['57939553_675562008']
  }
})['57939553_675562008']['market_hash_name'];

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question