my json give as output string and not int in python
import pathlib a=pathlib.Path(__file__).parent.absolute() dirc=str(a)+'\\file.json' dirc2=str(a)+'\\PrettyJson.json' data={1: {"titolo": "yea boi", "voto": 10, "genere": "a me ne so"}, 2: {"titolo": "yea boi 2", "voto": 8, "genere": "bo"}} def jsonPrettyPrint(): with open(dirc,'w') as json_file: json.dump(data, json_file) with open(dirc) as json_file: with open(dirc2,'w') as PrettyJsonFile: Obj = json.load(json_file) PrettyJson = json.dumps(Obj, indent=4) json.dump(PrettyJson,PrettyJsonFile) print(PrettyJson) jsonPrettyPrint()
Here the code, it work properly, but when i print Pretty Json, it give as output this
{ "1": { "titolo": "yea boi", "voto": 10, "genere": "a me ne so" }, "2": { "titolo": "yea boi 2", "voto": 8, "genere": "bo" } }
as u can see, 2 and 1 are strings and not intengers, but 8 and 10 are intengers, idk why, any help would be appreciated
Convert the keys to int after loading the JSON.
with open(dirc) as json_file: data = json.load(json_file) for key, value in list(data.items()): data[int(key)] = value del data[key] print(data)