Python Edit/Rename Key Names in .json
Asked Answered
R

4

13

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:
Really answered 11/4, 2018 at 14:36 Comment(1)
Your data is a dictionary. help(dict). Then maybe dump the JSON back.Contactor
M
35

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}
Manzanilla answered 11/4, 2018 at 14:39 Comment(3)
d['new_name'] = d.pop('old_name') Thank you so much it workedReally
Thanks! this helped.Biak
seems strange :)Sovran
F
9

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.

Fluctuant answered 6/12, 2018 at 7:45 Comment(1)
FYI RuntimeError: dictionary keys changed during iterationBeuthen
H
3

Something like

your_dict[new_key] = your_dict.pop(old_key)

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

Homolographic answered 11/4, 2018 at 14:39 Comment(0)
D
2

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..!!

Debutante answered 9/5, 2020 at 4:37 Comment(2)
I wouldn't suggest this method because it also considers the substring & replaces it as wellShiau
This is very dangerous considering the text inside any field in the objects.Imbrication

© 2022 - 2024 — McMap. All rights reserved.