Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Edit/Rename Key Names in .json

Is there a way to change the Key name? I need to change the name "Class123" like it is in the Example. I can change the Value but I don't know how to change the key name.

Example .json :

{
    "Class123": "classvalue", 
    "name1": {
        "name2": {
            "name3": {
                "Y": 158.8, 
                "X": 201.46
            }, 
            "name4": {
                "Y": 159.68, 
                "X": 200.32
            }
        }
    }
}

Starting like this:

with open('my.json') as json1:
    data = json.load(json1)
    for item in data:
like image 688
Kaira Asia Avatar asked Apr 11 '18 14:04

Kaira Asia


4 Answers

There is no way to "change" a key name. The best you can do is to copy the value to another key by using pop:

d = {'old_name': 1}
d['new_name'] = d.pop('old_name')
print(d)
# {'new_name': 1}
like image 192
DeepSpace Avatar answered Sep 28 '22 04:09

DeepSpace


I'd like to add my method here since it expands the singular naming to using a dict of keys.

Lets say you have a sample JSON list ga_list and you would like to change all the names with a dict key names_key :

names_key = { 'ga:date'            : 'date' ,
              'ga:bounceRate'      : 'bounce' ,
              'ga:newUsers'        : 'new_users', 
              'ga:pageviews'       : 'page_views',
              'ga:sessions'        : 'sessions',
              'ga:uniquePageviews' : 'unique_page_views',
              'ga:users'           : 'users'
              }

ga_list = [{
      'ga:bounceRate': 34.478408,
      'ga:date': '20151203',
      'ga:newUsers': 1679,
      'ga:pageviews': 550,
      'ga:sessions': 307,
      'ga:uniquePageviews': 467,
      'ga:users': 256},
    {'ga:bounceRate': 21.28534,
      'ga:date': '20151204',
      'ga:newUsers': 164,
      'ga:pageviews': 594,
      'ga:sessions': 305,
      'ga:uniquePageviews': 476,
      'ga:users': 252},
    {'ga:bounceRate': 13.8372346,
      'ga:date': '20151205',
      'ga:newUsers': 152,
      'ga:pageviews': 826,
      'ga:sessions': 330,
      'ga:uniquePageviews': 241,
      'ga:users': 200}]

for row in ga_list:
  for k, v in names_key.items():
    for old_name in row:
      if k == old_name:
        row[v] = row.pop(old_name)

print(ga_list)

>>>
[{'bounce': 34.47842608,
  'date': '20151203',
  'new_users': 1679,
  'page_views': 550,
  'sessions': 307,
  'unique_page_views': 467,
  'users': 256},
 {'bounce': 21.28534,
  'date': '20151204',
  'new_users': 164,
  'page_views': 594,
  ....
  'unique_page_views': 241,
  'users': 200}]

And there you have it all the old names are renamed according to the key.

Hope this helps someone.

like image 26
Mike G Avatar answered Sep 28 '22 04:09

Mike G


Something like

your_dict[new_key] = your_dict.pop(old_key)

Removing the old key from your dict and creating a new one.

like image 44
Freddy Avatar answered Sep 28 '22 04:09

Freddy


One way you can do it is with replace() function, but make sure that you have to convert your JSON Dict in to String first as Dictionary's key cannot be changed.


json_data = json_data.replace('key_old1','Key_new1').replace('key_old2','key_new2')

Hope this helps. Cheers..!!

like image 27
Saurabh Bade Avatar answered Sep 28 '22 05:09

Saurabh Bade