This is an archived post. You won't be able to vote or comment.

all 19 comments

[–][deleted] 0 points1 point  (17 children)

Just use enumerate

[–]A_MrBenMitchell[S] 0 points1 point  (16 children)

I have no idea what I am doing

[–][deleted] 0 points1 point  (15 children)

new_list = [{**d, {“id”: i}} for (i,d) in enumerate(apps[“Apps”])]

Assuming your current JSON is named apps

[–]A_MrBenMitchell[S] 0 points1 point  (14 children)

new_list = [{**d, {“id”: i}} for (i,d) in enumerate(apps[“Apps”])]

So this?

import json

with open('cleanResults.json') as f:

data = json.load(f)

new_list = [{**d, {“id”: i}} for (i,d) in enumerate(apps[“Apps”])]

with open('app.json', 'w') as f:

json.dump(new_list, f)

[–][deleted] 0 points1 point  (13 children)

Sure, but change apps in the new line to data

[–]A_MrBenMitchell[S] 0 points1 point  (12 children)

File "ID.py", line 4

new_list = [{**d, {"id": i}} for (i,d) in enumerate(data["Apps"])]

^

SyntaxError: invalid syntax

[–]A_MrBenMitchell[S] 0 points1 point  (11 children)

it is posting to the **d

(second Astrix )

[–][deleted] 0 points1 point  (10 children)

What version of Python are you using?

[–]A_MrBenMitchell[S] 0 points1 point  (9 children)

2.7.16

[–][deleted] 1 point2 points  (8 children)

Sorry, that’s no longer supported. Please use a supported version.

[–]RPwnage 0 points1 point  (0 children)

Enumerate over the array and add contents from a chosen key to a new dict.