diff options
-rw-r--r-- | .gitignore | 6 | ||||
-rw-r--r-- | README.md | 24 | ||||
-rwxr-xr-x | j2c.py | 78 | ||||
-rwxr-xr-x | json-to-csv.py | 38 |
4 files changed, 146 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..4cbbdf3 --- /dev/null +++ b/.gitignore @@ -0,0 +1,6 @@ +__pycache__ +*.2021* +*foo* +*.csv +*.json +*.xlsx diff --git a/README.md b/README.md new file mode 100644 index 0000000..018dcc2 --- /dev/null +++ b/README.md @@ -0,0 +1,24 @@ +# Readme for j2c +This package exists because I wanted to convert json to csv using python. Some hospitals use json as the "machine-readable" format for the hospital pricing transparency report. + +## Upstream +None + +## Alternatives +I am sure there are plenty of ways to convert json to csv, including even in python. + +## How to use +Run `./json-to-csv.py --help` to learn how to use the cli. + +The j2c file is the library, should anybody need that. + +## Dependencies +Python3 + +## References +### Hospital pricing transparency law +[https://www.cms.gov/hospital-price-transparency/hospitals](https://www.cms.gov/hospital-price-transparency/hospitals) +[https://www.federalregister.gov/documents/2019/11/27/2019-24931/medicare-and-medicaid-programs-cy-2020-hospital-outpatient-pps-policy-changes-and-payment-rates-and#h-84](https://www.federalregister.gov/documents/2019/11/27/2019-24931/medicare-and-medicaid-programs-cy-2020-hospital-outpatient-pps-policy-changes-and-payment-rates-and#h-84) + +## Differences from upstream +None @@ -0,0 +1,78 @@ +#!/usr/bin/env python3 +# File: j2c.py +# Location: https://gitlab.com/bgstack15/j2c +# Author: bgstack15 +# Startdate: 2021-01-16 +# SPDX-License-Identifier: CC-BY-SA-4.0 +# Title: Json To CSV Library for Python +# Purpose: NIH +# History: +# Usage: +# Reference: +# Improve: +# Documentation: +# vim: set ts=3 sw=3 sts=3 et: +import json, sys, os + +# default debug level for this library +debug = 0 + +def load_json_file(filename): + _lof=[] + with open(filename,"r") as f: + _lof = json.load(f) + return _lof + +def debugprint(dstring,debug=debug): + if debug >= 1: + print(dstring) + +def print_json(json_obj,indent=1): + print(json.dumps(json_obj,indent=indent)) + +def convert_json_to_csv(json_objorfile,csvfile="stdout",debug=debug): + + try: + if os.path.exists(json_objorfile): + json_obj = load_json_file(json_objorfile) + except: + json_obj = json_objorfile + print("json_obj=",json_obj) + + # Learn keys at this level + keys=[] + for i in json_obj: + for j in list(i.keys()): + if j not in keys: + keys.append(j) + debugprint("Found key {0}".format(j),debug=debug) + + #for k in keys: + # print(k) + + x=0 + fullstring="" + for i in json_obj: + x += 1 + # only if there are actually contents of "all" do we print the headers + if x == 1: + for k in keys: + fullstring += str(k) + "," + fullstring += "\n" + for k in keys: + p = "" + try: + p = str(i[k]).replace(",",":") + except: + # no value for this key for this entry + pass + pp = "{0},".format(p) + fullstring += pp + fullstring += "\n" + + if fullstring != "": + if csvfile == "stdout": + print(fullstring) + else: + with open(csvfile,"a") as of: + of.write(fullstring) diff --git a/json-to-csv.py b/json-to-csv.py new file mode 100755 index 0000000..0ae022d --- /dev/null +++ b/json-to-csv.py @@ -0,0 +1,38 @@ +#!/usr/bin/env python3 +# File: json-to-csv.py +# Location: https://gitlab.com/bgstack15/j2c +# Author: bgstack15 +# Startdate: 2021-01-16 +# SPDX-License-Identifier: CC-BY-SA-4.0 +# Title: Json to CSV cli utility +# Purpose: Front-end logic and argument parser +# History: +# Usage: +# Reference: +# Improve: +# Dependencies: +# j2c.py in this package +# Documentation: +from j2c import * +import argparse +debug = 0 + +parser = argparse.ArgumentParser() +parser.add_argument("-d","--debug", nargs='?', default=0, type=int, choices=range(0,11),help="Set debug level.") +parser.add_argument("-i","--infile", help="Json file to convert") +parser.add_argument("-o","--outfile", help="Csv file to output") +args = parser.parse_args() + +debug = args.debug if args.debug != None else 0 +if args.infile is None: + print("Need -i infile! Aborted",file=sys.stderr) + sys.exit(1) +else: + jsonfile = args.infile + +if args.outfile is None: + outfile = "stdout" +else: + outfile = args.outfile + +convert_json_to_csv(jsonfile,outfile,debug=debug) |