Skip to content

Instantly share code, notes, and snippets.

@FMNSSun
Last active April 12, 2016 13:50
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 FMNSSun/73700ffd1f6e176bc80960e60cfe4814 to your computer and use it in GitHub Desktop.
Save FMNSSun/73700ffd1f6e176bc80960e60cfe4814 to your computer and use it in GitHub Desktop.
Convert json array to newline separated
#
#- arr2nljson
# munt, 2016
#
# Reads in a JSON array and outputs new-line
# separated documents
#
#
import sys
import json
p_fin = sys.argv[1]
p_fout = sys.argv[2]
print("Input: " + p_fin)
print("Output: " + p_fout)
print("Opening files...")
fin = open(p_fin, "r")
fout = open(p_fout, "w")
print("Reading input...")
data = fin.read()
print("Loading json...")
json_data = json.loads(data)
n = 0
print("Writing json...")
for entry in json_data:
fout.write(json.dumps(entry, sort_keys = True) + "\n")
n += 1
print("Closing files...")
fin.close()
fout.close()
print("Done...")
print("Wrote " + str(n) + " entries.")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment