0

I have a folder containing ~10000 JSON files, and I'm looking to add a new identical key/value pair ("Symbol": "PPF") to each one.

Each JSON file is named using an increasing number: 0.json, 1.json, 2.json, ... 9999.json, 10000.json

My current best attempt was trying to remove the last line of every file (the final }), then append ,"Symbol": "PPF".

What would be the fastest way of adding that key/value pair to all JSON files?

Astro Orbis
  • 67
  • 2
  • 12

1 Answers1

1

Try the following looping over the files:

# Read a json file temp.json and convert to Python dict

import json

with open('temp.json') as f:
    data = json.load(f)

data["key"] = "value"

# Write back to file

with open('temp.json', 'w') as f:
    json.dump(data, f)

Altaf Shaikh
  • 272
  • 1
  • 6