I have a JSON file that looks like this:
"153689763780624385": {
"1": {
"author": "Mike",
"date": "05/06/2022, 12:00:57"
},
"2": {
"author": "Mike",
"date": "05/06/2022, 13:29:43"
},
"3": {
"author": "Jan01p",
"date": "05/7/2022, 16:01:22"
}
}
What I am doing is accessing my JSON, and deleting the 2nd object key in the json file. With the following code.
with open("pins.json", "r+") as f:
p = json.load(f)
gid = "153689763780624385"
del p[gid]["2"]
with open("pins.json", "w+") as fp:
json.dump(p, fp, indent=4)
Now my JSON looks like this
"153689763780624385": {
"1": {
"author": "Mike",
"date": "05/06/2022, 12:00:57"
}
"3": {
"author": "Jan01p",
"date": "05/7/2022, 16:01:22"
}
}
My goal here is to use a loop of some sort to rename all the following keys to be in order. So change 3
to 2
. Name 4
to 3
and so on. The JSON is much larger just made it shorter for asking purposes. What's a good way to rename the keys to go in incrementing order? So far i'm thinking of dict.pop in a for loop. But can't figure it out.