Change specific element in json file in python

I am working on a json file which consists of different data like this:

[{
  "_id": {
    "$oid": "60de048cd7905488bbe0e511"
  },
  "item_name": "STOCKHOLM",
  "item_type": "Sofa",
  "item_id": "302.450.44",
  "item_detail": [
    "3paxsofa",
    "seglora"
  ],
  "item_price": "18990",
  "item_count": "35",
  "item_size": {
    "width:": "211 cm",
    "depth:": "88 cm",
    "height:": "80 cm",
    "heigth2:": "14 cm",
    "height3:": "72 cm",
    "depth2:": "158 cm",
    "depth3:": "59 cm",
    "height4:": "43 cm",
    "no:": "3 "
  },
  "item_collapsepk": {
    "product_id: ": "302.450.44",
    "pack: ": "1",
    "depth: ": "216 cm",
    "width: ": "92 cm",
    "height: ": "60 cm",
    "total_weight: ": "71.26 kg",
    "net_weight: ": "56.64 kg",
    "pack_per: ": "1176.0 L"
  },
  "maintenance": [
    "avoid sunset ",
    "clear with wet towel"
  ],
  "item_good": [
    "natural leather ",
    "softer leathwe ",
    "depper color and longer lifespan",
    "Niels Gammelgaard"
  ],
  "item_env": {}
}
]
 

How can I delete all the “cm” in item_size from the json file and save back to the oringinal file in order to have a better data for further usage. And getting the result like this one:

"item_size": {
    "width:": "211 ",
    "depth:": "88 ",
    "height:": "80 ",
    "heigth2:": "14 ",
    "height3:": "72 ",
    "depth2:": "158 ",
    "depth3:": "59 ",
    "height4:": "43 ",
    "no:": "3 "
  }

Answer

import json
data="""[{
  "_id": {
    "$oid": "60de048cd7905488bbe0e511"
  },
  "item_name": "STOCKHOLM",
  "item_type": "Sofa",
  "item_id": "302.450.44",
  "item_detail": [
    "3paxsofa",
    "seglora"
  ],
  "item_price": "18990",
  "item_count": "35",
  "item_size": {
    "width:": "211 cm",
    "depth:": "88 cm",
    "height:": "80 cm",
    "heigth2:": "14 cm",
    "height3:": "72 cm",
    "depth2:": "158 cm",
    "depth3:": "59 cm",
    "height4:": "43 cm",
    "no:": "3 "
  },
  "item_collapsepk": {
    "product_id: ": "302.450.44",
    "pack: ": "1",
    "depth: ": "216 cm",
    "width: ": "92 cm",
    "height: ": "60 cm",
    "total_weight: ": "71.26 kg",
    "net_weight: ": "56.64 kg",
    "pack_per: ": "1176.0 L"
  },
  "maintenance": [
    "avoid sunset ",
    "clear with wet towel"
  ],
  "item_good": [
    "natural leather ",
    "softer leathwe ",
    "depper color and longer lifespan",
    "Niels Gammelgaard"
  ],
  "item_env": {}
}
]"""
myvar=json.loads(data)
for i in myvar[0]['item_size'].items():
    if 'cm' in i[1]: 
        myvar[0]['item_size'][i[0]]=i[1].replace('cm','').strip()

for i in myvar[0]['item_collapsepk'].items():
    if 'cm' in i[1]: 
        myvar[0]['item_collapsepk'][i[0]]=i[1].replace('cm','').strip()
  1. Assumed you have only one list item, so used myvar[0].
  2. i variable is a tuple with (Key, value) pair in it which we got from .items().
  3. if is written to check if the values have cm in it. regular expressions can be used if you have a more sophisticated scenario.
  4. Assigned a new value to the same key and used the string replace method to remove the cm from the text & used strip method to remove the spaces