diff --git a/json2html/jsonconv.py b/json2html/jsonconv.py index cf37df8..9057635 100644 --- a/json2html/jsonconv.py +++ b/json2html/jsonconv.py @@ -1,3 +1,5 @@ +# -*- coding: utf-8 -*- + ''' JSON 2 HTML convertor ===================== @@ -5,17 +7,15 @@ (c) Varun Malhotra 2013 Source Code: https://github.com/softvar/json2html ------------ +Contributors: Michel Müller(@muellermichel), patch #2 - https://github.com/softvar/json2html/pull/2 LICENSE: MIT -------- ''' -# -*- coding: utf-8 -*- import json import ordereddict -a= '' - class JSON: #def __init__(self): @@ -23,6 +23,12 @@ def convert(self,**args): ''' convert json Object to HTML Table format ''' + global table_attributes + table_attributes = '' + if 'table_attributes' in args: + table_attributes = args['table_attributes'] + else: + table_attributes = "border=\"1\"" if 'json' in args: self.json_input = args['json'] try: @@ -31,66 +37,77 @@ def convert(self,**args): self.json_input = json.dumps(self.json_input) else: raise Exception('Can\'t convert NULL!') - - + + ordered_json = json.loads(self.json_input, object_pairs_hook=ordereddict.OrderedDict) return self.htmlConvertor(ordered_json) + def columnHeadersFromListOfDicts(self,ordered_json): + if len(ordered_json) < 2: + return None + if not isinstance(ordered_json[0],dict): + return None + column_headers = ordered_json[0].keys() + for entry in ordered_json: + if not isinstance(entry,dict): + return None + if len(entry.keys()) != len(column_headers): + return None + for header in column_headers: + if not header in entry: + return None + return column_headers + #ordered_json needs to be a dict def iterJson(self,ordered_json): - global a - a=a+ "" + def markup(entry, parent_is_list=False): + if(isinstance(entry,unicode)): + return unicode(entry) + if(isinstance(entry,int) or isinstance(entry,float)): + return str(entry) + if(parent_is_list and isinstance(entry,list)==True): + #list of lists are not accepted + return '' + if(isinstance(entry,list)==True) and len(entry) == 0: + return '' + if(isinstance(entry,list)==True): + return '' + if(isinstance(entry,dict)==True): + return self.iterJson(entry) + return '' #safety: don't do recursion over anything that we don't know about - iteritems() will most probably fail + + a = '' + global table_attributes + + table_init_markup = "
" %(table_attributes) + a=a+ table_init_markup for k,v in ordered_json.iteritems(): a=a+ '' a=a+ '' if (v==None): - v = unicode("") + v = unicode("") if(isinstance(v,list)): - a=a+ '' - a=a+ '' - elif(isinstance(v,unicode)): - a=a+ '' - a=a+ '' - elif(isinstance(v,int) or isinstance(v,float)): - a=a+ '' - a=a+ '' - else: - a=a+ '' + a=a+ '' + continue + a=a+ '' + a=a+ '' a=a+ '
'+ str(k) +'
    ' - for i in range(0,len(v)): - if(isinstance(v[i],unicode)): - a=a+ '
  • '+unicode(v[i])+'
  • ' - elif(isinstance(v[i],int) or isinstance(v,float)): - a=a+ '
  • '+str(v[i])+'
  • ' - elif(isinstance(v[i],list)==False): - self.iterJson(v[i]) - a=a+ '
'+ unicode(v) +'
'+ str(v) +'
' - #a=a+ '' - self.iterJson(v) - a=a+ '' + column_headers = self.columnHeadersFromListOfDicts(v) + if column_headers != None: + a=a+ '' + for list_entry in v: + a=a+ '' + a=a+ '
' + a=a+ table_init_markup + a=a+ '
' + ''.join(column_headers) + '
' + ''.join([markup(list_entry[column_header]) for column_header in column_headers]) + '
' + a=a+ '
' + markup(v) + '
' + return a def htmlConvertor(self,ordered_json): ''' converts JSON Object into human readable HTML representation generating HTML table code with raw/bootstrap styling. ''' - global a - a='' - try: - for k,v in ordered_json.iteritems(): - pass - self.iterJson(ordered_json) - - except: - for i in range(0,len(ordered_json)): - if(isinstance(ordered_json[i],unicode)): - a=a+ '
  • '+unicode(ordered_json[i])+'
  • ' - elif(isinstance(ordered_json[i],int) or isinstance(ordered_json[i],float)): - a=a+ '
  • '+str(ordered_json[i])+'
  • ' - elif(isinstance(ordered_json[i],list)==False): - self.htmlConvertor(ordered_json[i]) - - return a - + return self.iterJson(ordered_json) -json2html = JSON() \ No newline at end of file +json2html = JSON()