Created
December 17, 2015 17:56
-
-
Save jackschultz/b5f09b99e09174ad5a25 to your computer and use it in GitHub Desktop.
Accepts json string, reformats the data and matches variable names with arrays of their values according to certain rules. Can be used from command line by running "python recombinator.py STRING" where STRING is valid json, or it can be imported and run with other python code.
This file contains hidden or 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
import sys | |
import json | |
from collections import defaultdict | |
def parse_list_based_json(data): | |
out = {} | |
for index, value in enumerate(data[0]): | |
out[value] =[row[index] for row in data[1:]] | |
return out | |
def parse_dict_based_json(data): | |
#Loops the rows, and creates a set of all the keys. | |
#We need the keys to know where to put in None values | |
keys = set([key for row in data for key in row]) | |
out = defaultdict(list) | |
for row in data: | |
[out[key].append(row[key] if key in row else None) for key in keys] | |
return out | |
def format_and_print(output): | |
print "'" + json.dumps(output) + "'" | |
def recombinate(data_string): | |
try: | |
data = json.loads(data_string) | |
except ValueError: | |
print "Error loading json string" | |
return | |
if type(data) is not list: | |
print "Error: Loaded json is not array like required" | |
return | |
if not len(data): | |
print "Error: Loaded json array has no length" | |
return | |
#should be good to go | |
if type(data[0]) is list: | |
format_and_print(parse_list_based_json(data)) | |
elif type(data[0]) is dict: | |
format_and_print(parse_dict_based_json(data)) | |
else: | |
print "Error with type of object in 0th index" | |
return #also an error check | |
if __name__ == "__main__": | |
try: | |
recombinate(sys.argv[1]) | |
except IndexError: | |
print "Please pass in argument string" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment