I have the following JSON:
{
"1605855600000":
[
{
"id": "1",
"value": "1. Choice 1",
"checked": true
},
{
"id": "2",
"value": "2. Choice 2",
"checked": false
},
{
"id": "3",
"value": "3. Choice 3",
"checked": false
},
{
"id": "4",
"value": "4. Choice 4",
"checked": false
},
{
"id": "5",
"value": "5. Choice 5",
"checked": false
}
],
"1604732400000":
[
{
"id": "1",
"value": "1. Choice 1",
"checked": false
},
{
"id": "2",
"value": "2. Choice 2",
"checked": false
},
{
"id": "3",
"value": "3. Choice 3",
"checked": false
},
{
"id": "4",
"value": "4. Choice 4",
"checked": false
},
{
"id": "5",
"value": "5. Choice 5",
"checked": false
}
]
}
I'm not sure the exact terminology but the "keys" like '1605855600000' and '1604732400000' are UNIX timestamps that I need to convert in Python.
Here is what I have so far:
def convert_timestamp(obj):
for key in obj.keys():
timestamp = int(key) / 1000
dt_object = datetime.fromtimestamp(timestamp)
date = dt_object.strftime("%B %d, %Y")
new_key = date
if new_key != key:
obj[new_key] = obj[key]
del obj[key]
return obj
data = json.loads(data, object_hook=convert_timestamp)
However, the error I receive says:
ValueError: invalid literal for int() with base 10: 'id'
which means that it is accessing a level below what I am trying to change.
I know the logic inside the definition works to output the string that I need as the final result, but accessing and replacing those with the current logic isn't working.
The final output needs to look something like:
{
"12 November, 2020":
[
{
"id": "1",
"value": "1. Choice 1",
"checked": true
},
...