Search code examples
pythonpandasdataframedictionarylowercase

lowercase all the letters in json


My json file looks like this-

{"classes":["BUSINESS","PLACE","HOLD","ROAD","SUB","SUPER","EA","DIS","SUB","UNI"],"annotations":[["Khilg Khil Block A Dha M K Enter House 308/A Road 8 Til Rhy", {"entities": [[0, 8, "EA"], [9, 17, "SUB"], [18, 25, "SUPER"], [26, 31, "DIS"], [32, 46, "BUSINESS"], [47, 58, "HOLDING"], [59, 65, "ROAD"], [66, 75, "SUB"], [76, 82, "PLACE"]]}]

I want to make every letter lowercase here in this case for a full file. But it's not working. My code-

data = json.load(open("data.json"))
new_data = {}

for i in new_data.keys():
    if type(new_data[i]) is list:
        new_data[i]=  [j.lower() for j in new_data[i]]
    else:
        new_data[i] = new_data[i].lower()

with open("data.json", 'w') as fp:
    json.dump(new_data, fp)

Solution

  • Since there are lists of lists in your data, i think it is better to use recursion.

    Code:

    def recursion_lower(x):
        if type(x) is str:
            return x.lower()
        elif type(x) is list:
            return [recursion_lower(i) for i in x]
        elif type(x) is dict:
            return {recursion_lower(k):recursion_lower(v) for k,v in x.items()}
        else:
            return x
    
    data = {'classes': ['BUSINESS', 'PLACE', 'HOLD', 'ROAD', 'SUB', 'SUPER', 'EA', 'DIS', 'SUB', 'UNI'], 'annotations': [['Khilg Khil Block A Dha M K Enter House 308/A Road 8 Til Rhy', {'entities': [[0, 8, 'EA'], [9, 17, 'SUB'], [18, 25, 'SUPER'], [26, 31, 'DIS'], [32, 46, 'BUSINESS'], [47, 58, 'HOLDING'], [59, 65, 'ROAD'], [66, 75, 'SUB'], [76, 82, 'PLACE']]}]]}
    
    new_data = recursion_lower(data)
    print(new_data)
    

    Output:

    {'classes': ['business', 'place', 'hold', 'road', 'sub', 'super', 'ea', 'dis', 'sub', 'uni'], 'annotations': [['khilg khil block a dha m k enter house 308/a road 8 til rhy', {'entities': [[0, 8, 'ea'], [9, 17, 'sub'], [18, 25, 'super'], [26, 31, 'dis'], [32, 46, 'business'], [47, 58, 'holding'], [59, 65, 'road'], [66, 75, 'sub'], [76, 82, 'place']]}]]}