Skip to content

Instantly share code, notes, and snippets.

@rotated8
Last active August 18, 2017 15:01
Show Gist options
  • Save rotated8/496fca676da14d19c320906483126b57 to your computer and use it in GitHub Desktop.
Save rotated8/496fca676da14d19c320906483126b57 to your computer and use it in GitHub Desktop.
Convert CSV or Database data to JSON
import cx_Oracle as oracle
import argparse, csv, json, os, os.path
QUERY = 'SELECT * FROM ESDV.V_ETD_EMPE'
ROW_KEY = 'ETD Record Key'
def from_database(credentials):
data = {}
with oracle.connect(credentials) as conn:
cursor = conn.cursor()
for row in cursor.execute(QUERY):
ppid = row[0]
data.setdefault(ppid, {})
# The cursor.description includes a bunch of info for each column; first is the name.
column_names = [x for x in map(lambda l: l[0], cursor.description)]
for column_name, row_value in zip(column_names, row):
data[ppid].setdefault(column_name, []).append(row_value)
return data
def from_csv(csv_path, csv_encoding='utf-8'):
data = {}
csv_path = os.path.abspath(csv_path)
with open(csv_path, encoding=csv_encoding) as csv_file:
csv_reader = csv.DictReader(csv_file)
for row in csv_reader:
data[row[ROW_KEY]] = row
return data
def export(output_path, data):
output_path = os.path.abspath(output_path)
with open(output_path, 'w') as output_file:
json.dump(data, output_file, ensure_ascii=False, indent=2)
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Convert data to json.')
parser.add_argument('output', help='Path to the output file')
parser.add_argument('--csv-path', '-c', help='Path to the CSV file to convert')
parser.add_argument('--database', '-d', help='Pull data from a database.', action='store_true')
args = parser.parse_args()
if args.database:
export(args.output, from_database(os.environ['ETD_ESD_CONNECTION']))
elif args.csv_path:
export(args.output, from_csv(args.csv_path))
else:
print('Please specify one of the available data sources via arguments')
parser.print_help()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment