Convert json array to newline separated
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
#- 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