How can you change keys in a json object?

  • 0
    "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 this?
    JavaScript Anonymous, Dec 9, 2020

  • 3 Answers
  • 0
    1) replace text with simple replacement or regular expression;

    2) unpack, rename the field, pack back.
    Anonymous

  • 0
    Parse the JSON string into a regular object, change, "pack", as described above, back.



    Here are the docs: JSON format (RU)
    Anonymous

  • 0
    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'];
    Anonymous

Your Answer
To place the code, please use CodePen or similar tool. Thanks you!