Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I edit/rename keys during json.load in python?

I have a json file ( ~3Gb ) that I need to load into mongodb. Quite a few of the json keys contain a . (dot), which causes the load into mongodb to fail. I want to the load the json file, and edit the key names in the process, say replace the dot with an empty space. Using the following python code

import json

def RemoveDotKey(dataPart):
    for key in dataPart.iterkeys():
        new_key = key.replace(".","")
        if new_key != key:
            newDataPart = deepcopy(dataPart)
            newDataPart[new_key] = newDataPart[key]
            del newDataPart[key]
            return newDataPart
    return dataPart

new_json = json.loads(data, object_hook=RemoveDotKey) 

The object_hook called RemoveDotKey should iterate over all the keys, it a key contains a dot, create a copy, replace the dot with a space, and return the copy. Created a copy of dataPart, since not sure if I can iterate over dataPart's keys and insert/delete key value pairs at the same time.

There seems to be an error here, all the json keys with a dot in them are not getting edited. I am not very sure how json.load works. Also am new to python ( been using it for less than a week )

like image 264
okaytee Avatar asked Jun 25 '12 11:06

okaytee


1 Answers

You almost had it:

import json

def remove_dot_key(obj):
    for key in obj.keys():
        new_key = key.replace(".","")
        if new_key != key:
            obj[new_key] = obj[key]
            del obj[key]
    return obj

new_json = json.loads(data, object_hook=remove_dot_key) 

You were returning a dictionary inside your loop, so you'd only modify one key. And you don't need to make a copy of the values, just rename the keys.

like image 67
Ned Batchelder Avatar answered Sep 26 '22 13:09

Ned Batchelder