Skip to content

Instantly share code, notes, and snippets.

@SIRHAMY
Created April 19, 2016 04:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SIRHAMY/0d0c112118a8e07f8a5ca367bf6cd587 to your computer and use it in GitHub Desktop.
Save SIRHAMY/0d0c112118a8e07f8a5ca367bf6cd587 to your computer and use it in GitHub Desktop.
import json
#This is going to fetch our lines one-by-one
#Useful for super-huge files
class JSONFetcher():
def __init__(self, fileName):
self.fileName = fileName + '.json'
def fetch(self):
print("fetching...")
with open(self.fileName, 'rb') as f:
for line in f:
yield json.loads(line)
if __name__ == "__main__":
filename = "MY_FILENAME"
sourcefile = JSONFetcher(filename)
outfile = open("OUTFILE", 'wb')
for record in sourcefile.fetch():
record['ADDED_KEY'] = "NEW_VALUE_HERE"]
jsonOut = json.dumps(record)
outfile.write(jsonOut + '\n')
outfile.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment