Last active
August 29, 2015 14:07
-
-
Save sixmanguru/848e4a9440a640c4543a to your computer and use it in GitHub Desktop.
JSON to Excel converter
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
##Tool to turn all of the values of a JSON object into | |
##an easy-to-read list | |
##with Path(key),Value | |
##some of the subject matter discussed pertains with my company and may not seem relevant | |
import itertools | |
import csv, xlrd, codecs, xlwt | |
import json | |
##set FULL or ENDPOINT for path | |
##FULL actually cuts out first two depths (d) from the path, i.e. | |
##OrderProductResponse.OrderProductResponseDetail. | |
##FULL is recommended for Direct | |
##ENDPOINT for Toolkit | |
myPath = 'ENDPOINT' | |
d = 2 | |
def leaf_value(obj, prefix=''): | |
if isinstance(obj, dict): | |
for k, v in obj.items(): | |
for res in leaf_value(v, str(v)): | |
yield res | |
elif isinstance(obj, list): | |
for i, v in enumerate(obj): | |
for res in leaf_value(v, '['+str(v)+']'): | |
yield res | |
else: | |
yield prefix | |
def dot_notation(obj, prefix=''): | |
if isinstance(obj, dict): | |
if prefix: prefix += '.' | |
for k, v in obj.items(): | |
for res in dot_notation(v, prefix+str(k)): | |
yield res | |
elif isinstance(obj, list): | |
for i, v in enumerate(obj): | |
for res in dot_notation(v, prefix+'['+str(i)+']'): | |
yield res | |
else: | |
yield prefix | |
##read in the JSON | |
##This could easily be modified to make a call | |
##for help converting SOAP responses to JSON, | |
##try the website http://www.utilities-online.info/xmltojson/ | |
##for easy conversion | |
startFileName = 'yourJSONfile.txt' | |
filename = startFileName | |
inputFile = open(filename).read() | |
data = json.loads(inputFile) | |
##finds the path as a list | |
y = list(dot_notation(data)) | |
##returns the values as a list | |
x = list(leaf_value(data)) | |
##now to combine and output the data to Excel | |
wb = xlwt.Workbook() | |
sh = wb.add_sheet(filename) | |
for i in range(len(y)): | |
tmp = '' | |
parts = y[i].split('.') | |
for k in range(d,len(parts)): | |
tmp = tmp + parts[k] | |
if k != len(y[i])-1: tmp = tmp + '.' | |
##if the path is shorter than two for some reason (i.e. @ServiceVersionNumber in D2) | |
if len(parts)<=2: tmp = parts[len(parts)-1] | |
##FULL path or ENDPOINT | |
if myPath != 'FULL': | |
tmp = parts[len(parts)-1] | |
##if ENDPOINT, is it an array that needs more explanation | |
if '[' in tmp or '$' in tmp or '@' in tmp: tmp = parts[len(parts)-2] + '.' + parts[len(parts)-1] | |
sh.write(i,0,tmp) | |
sh.write(i,1,x[i]) | |
wb.save("newPathsValues.xls") | |
##This script is not efficient from a Big O standpoint. I know that. I was solving two | |
##independent problems then combining them. The next revision will fix this. | |
##Also I will add more flexibility for ENDPOINT. | |
##I would also like to acknowledge that in all reality, it uses FULL and | |
##anything not FULL (for ENDPOINT) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment