Skip to content

now with the coding hashbang at the top #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Feb 1, 2015
113 changes: 65 additions & 48 deletions json2html/jsonconv.py
Original file line number Diff line number Diff line change
@@ -1,28 +1,34 @@
# -*- coding: utf-8 -*-

'''
JSON 2 HTML convertor
=====================

(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):

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:
Expand All @@ -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+ "<table border=\"1\">"
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 '<ul><li>' + '</li><li>'.join([markup(child, parent_is_list=True) for child in entry]) + '</li></ul>'
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 %s>" %(table_attributes)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about if no table_attributes is/are present? Default table border has been overwritten. Also when I try the simple example:

_json2conv = {
    "glossary": {
        "title": "example glossary",
        "GlossDiv": {
            "title": "S",
            "GlossList": {
                "GlossEntry": {
                    "ID": "SGML",
                    "SortAs": "SGML",
                    "GlossTerm": "Standard Generalized Markup Language",
                    "Acronym": "SGML",
                    "Abbrev": "ISO 8879:1986",
                    "GlossDef": {
                        "para": "A meta-markup language, used to create markup languages such as DocBook.",
                        "GlossSeeAlso": ["GML", "XML"]
                    },
                    "GlossSee": "markup"
                }
            }
        }
    }
}

I didn't get the correct output. Please re-check what's being missing. Also, do share your json, I'm curious to see the sorting as suggested by you. It would be great to see everything working perfectly on my end. Thanks for introducing sort.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok, just committed another fix that readds the borders as default behavior. I’ve tested your json and to me it looks ok now. About my json: Here you go, it’s a doozy ;-).

{"errors": ["invalid frontend definition. Please check the domain and the characters in the name: frontend:myproject-oldtest4.myproject.com", "invalid frontend definition. Please check the domain and the characters in the name: frontend:myproject-oldtest5.myproject.com", "invalid frontend definition. Please check the domain and the characters in the name: frontend:myproject-oldtest6.myproject.com", "invalid frontend definition. Please check the domain and the characters in the name: frontend:ategra-test.myproject.com", "invalid frontend definition. Please check the domain and the characters in the name: frontend:myproject-oldtestjan1.myproject.com", "invalid frontend definition. Please check the domain and the characters in the name: frontend:www.myproject.com", "invalid frontend definition. Please check the domain and the characters in the name: frontend:acme.myproject.com", "invalid frontend definition. Please check the domain and the characters in the name: frontend:myproject.com", "invalid frontend definition. Please check the domain and the characters in the name: frontend:zh-bd.myproject.com", "invalid frontend definition. Please check the domain and the characters in the name: frontend:acme2.myproject.com", "invalid frontend definition. Please check the domain and the characters in the name: frontend:zh-vd.myproject.com", "invalid frontend definition. Please check the domain and the characters in the name: frontend:myproject-oldtest1.myproject.com", "invalid frontend definition. Please check the domain and the characters in the name: frontend:myproject-oldtest2.myproject.com", "invalid frontend definition. Please check the domain and the characters in the name: frontend:myproject-oldtest3.myproject.com", "ategra-test's metadata is invalid", "www's metadata is invalid", "acme's metadata is invalid", "myproject's metadata is invalid", "zh-bd's metadata is invalid", "acme2's metadata is invalid", "zh-vd's metadata is invalid", "no db backend defined for environment myproject-oldtestjan2. Client replication will not be available!", "no db backend defined for environment 09test1. Client replication will not be available!"], "environments": [{"status": "no-server-assigned", "web backends": [], "server": "undefined", "environment name": "myproject-oldtest4", "db backends": [], "email used for registration": "[email protected]"}, {"status": "no-server-assigned", "web backends": [], "server": "undefined", "environment name": "myproject-oldtest5", "db backends": [], "email used for registration": "[email protected]"}, {"status": "no-server-assigned", "web backends": [], "server": "undefined", "environment name": "myproject-oldtest7", "db backends": [], "email used for registration": "[email protected]"}, {"status": "no-server-assigned", "web backends": [], "server": "undefined", "environment name": "myproject-oldtest6", "db backends": [], "email used for registration": "[email protected]"}, {"status": "undefined", "web backends": [], "server": "undefined", "environment name": "ategra-test", "db backends": [], "email used for registration": "n/a"}, {"status": "no-server-assigned", "web backends": [], "server": "undefined", "environment name": "myproject-oldtestjan1", "db backends": [], "email used for registration": "[email protected]"}, {"status": "undefined", "web backends": [], "server": "undefined", "environment name": "www", "db backends": [], "email used for registration": "n/a"}, {"status": "undefined", "web backends": [], "server": "undefined", "environment name": "acme", "db backends": [], "email used for registration": "n/a"}, {"status": "no-server-assigned", "web backends": [], "server": "undefined", "environment name": "myproject-oldtest8", "db backends": [], "email used for registration": "[email protected]"}, {"status": "undefined", "web backends": [], "server": "undefined", "environment name": "myproject", "db backends": [], "email used for registration": "n/a"}, {"status": "undefined", "web backends": [], "server": "undefined", "environment name": "zh-bd", "db backends": [], "email used for registration": "n/a"}, {"status": "no-server-assigned", "web backends": [], "server": "undefined", "environment name": "myproject-oldtest10", "db backends": [], "email used for registration": "[email protected]"}, {"status": "undefined", "web backends": [], "server": "undefined", "environment name": "acme2", "db backends": [], "email used for registration": "n/a"}, {"status": "undefined", "web backends": [], "server": "undefined", "environment name": "zh-vd", "db backends": [], "email used for registration": "n/a"}, {"status": "no-server-assigned", "web backends": [], "server": "undefined", "environment name": "myproject-oldtest1", "db backends": [], "email used for registration": "[email protected]"}, {"status": "no-server-assigned", "web backends": [], "server": "undefined", "environment name": "myproject-oldtest2", "db backends": [], "email used for registration": "[email protected]"}, {"status": "no-server-assigned", "web backends": [], "server": "undefined", "environment name": "myproject-oldtest3", "db backends": [], "email used for registration": "[email protected]"}, {"status": "activated", "web backends": ["pgrid0.myproject-old.com:10001"], "server": "pgrid0", "environment name": "myproject-oldtestjan2", "db backends": [], "email used for registration": "[email protected]"}, {"status": "activated", "web backends": ["pgrid0.myproject-old.com:10002"], "server": "pgrid0", "environment name": "09test1", "db backends": [], "email used for registration": "[email protected]"}, {"status": "activated", "web backends": ["pgrid0.myproject-old.com:10003"], "server": "pgrid0", "environment name": "testnew20151", "db backends": ["pgrid0.myproject-old.com:11003"], "email used for registration": "[email protected]"}], "servers": ["pgrid0"]}
{"errors": ["invalid frontend definition. Please check the domain and the characters in the name: frontend:myproject-oldtest4.myproject.com", "invalid frontend definition. Please check the domain and the characters in the name: frontend:myproject-oldtest5.myproject.com", "invalid frontend definition. Please check the domain and the characters in the name: frontend:myproject-oldtest6.myproject.com", "invalid frontend definition. Please check the domain and the characters in the name: frontend:ategra-test.myproject.com", "invalid frontend definition. Please check the domain and the characters in the name: frontend:myproject-oldtestjan1.myproject.com", "invalid frontend definition. Please check the domain and the characters in the name: frontend:www.myproject.com", "invalid frontend definition. Please check the domain and the characters in the name: frontend:acme.myproject.com", "invalid frontend definition. Please check the domain and the characters in the name: frontend:myproject.com", "invalid frontend definition. Please check the domain and the characters in the name: frontend:zh-bd.myproject.com", "invalid frontend definition. Please check the domain and the characters in the name: frontend:acme2.myproject.com", "invalid frontend definition. Please check the domain and the characters in the name: frontend:zh-vd.myproject.com", "invalid frontend definition. Please check the domain and the characters in the name: frontend:myproject-oldtest1.myproject.com", "invalid frontend definition. Please check the domain and the characters in the name: frontend:myproject-oldtest2.myproject.com", "invalid frontend definition. Please check the domain and the characters in the name: frontend:myproject-oldtest3.myproject.com", "ategra-test's metadata is invalid", "www's metadata is invalid", "acme's metadata is invalid", "myproject's metadata is invalid", "zh-bd's metadata is invalid", "acme2's metadata is invalid", "zh-vd's metadata is invalid", "no db backend defined for environment myproject-oldtestjan2. Client replication will not be available!", "no db backend defined for environment 09test1. Client replication will not be available!"], "environments": [{"status": "no-server-assigned", "web backends": [], "server": "undefined", "environment name": "myproject-oldtest4", "db backends": [], "email used for registration": "[email protected]"}, {"status": "no-server-assigned", "web backends": [], "server": "undefined", "environment name": "myproject-oldtest5", "db backends": [], "email used for registration": "[email protected]"}, {"status": "no-server-assigned", "web backends": [], "server": "undefined", "environment name": "myproject-oldtest7", "db backends": [], "email used for registration": "[email protected]"}, {"status": "no-server-assigned", "web backends": [], "server": "undefined", "environment name": "myproject-oldtest6", "db backends": [], "email used for registration": "[email protected]"}, {"status": "undefined", "web backends": [], "server": "undefined", "environment name": "ategra-test", "db backends": [], "email used for registration": "n/a"}, {"status": "no-server-assigned", "web backends": [], "server": "undefined", "environment name": "myproject-oldtestjan1", "db backends": [], "email used for registration": "[email protected]"}, {"status": "undefined", "web backends": [], "server": "undefined", "environment name": "www", "db backends": [], "email used for registration": "n/a"}, {"status": "undefined", "web backends": [], "server": "undefined", "environment name": "acme", "db backends": [], "email used for registration": "n/a"}, {"status": "no-server-assigned", "web backends": [], "server": "undefined", "environment name": "myproject-oldtest8", "db backends": [], "email used for registration": "[email protected]"}, {"status": "undefined", "web backends": [], "server": "undefined", "environment name": "myproject", "db backends": [], "email used for registration": "n/a"}, {"status": "undefined", "web backends": [], "server": "undefined", "environment name": "zh-bd", "db backends": [], "email used for registration": "n/a"}, {"status": "no-server-assigned", "web backends": [], "server": "undefined", "environment name": "myproject-oldtest10", "db backends": [], "email used for registration": "[email protected]"}, {"status": "undefined", "web backends": [], "server": "undefined", "environment name": "acme2", "db backends": [], "email used for registration": "n/a"}, {"status": "undefined", "web backends": [], "server": "undefined", "environment name": "zh-vd", "db backends": [], "email used for registration": "n/a"}, {"status": "no-server-assigned", "web backends": [], "server": "undefined", "environment name": "myproject-oldtest1", "db backends": [], "email used for registration": "[email protected]"}, {"status": "no-server-assigned", "web backends": [], "server": "undefined", "environment name": "myproject-oldtest2", "db backends": [], "email used for registration": "[email protected]"}, {"status": "no-server-assigned", "web backends": [], "server": "undefined", "environment name": "myproject-oldtest3", "db backends": [], "email used for registration": "[email protected]"}, {"status": "activated", "web backends": ["pgrid0.myproject-old.com:10001"], "server": "pgrid0", "environment name": "myproject-oldtestjan2", "db backends": [], "email used for registration": "[email protected]"}, {"status": "activated", "web backends": ["pgrid0.myproject-old.com:10002"], "server": "pgrid0", "environment name": "09test1", "db backends": [], "email used for registration": "[email protected]"}, {"status": "activated", "web backends": ["pgrid0.myproject-old.com:10003"], "server": "pgrid0", "environment name": "testnew20151", "db backends": ["pgrid0.myproject-old.com:11003"], "email used for registration": "[email protected]"}], "servers": ["pgrid0"]}

On 01 Feb 2015, at 22:41, Varun Malhotra [email protected] wrote:

In json2html/jsonconv.py:

    global a
  •   a=a+ "<table border=\"1\">" 
    
  •   global table_attributes
    
  •   table_init_markup = "<table %s>" %(table_attributes)
    
    What about of no table_attributes is/are not present? Default table border has been overwritten. Also when I try the simple example:

_json2conv = {
"glossary": {
"title": "example glossary",
"GlossDiv": {
"title": "S",
"GlossList": {
"GlossEntry": {
"ID": "SGML",
"SortAs": "SGML",
"GlossTerm": "Standard Generalized Markup Language",
"Acronym": "SGML",
"Abbrev": "ISO 8879:1986",
"GlossDef": {
"para": "A meta-markup language, used to create markup languages such as DocBook.",
"GlossSeeAlso": ["GML", "XML"]
},
"GlossSee": "markup"
}
}
}
}
}
I didn't get the correct output. Please re-check what's being missing. Also, do share your json, I'm curious to see the sorting as suggested by you. It would be great to see everything working perfectly on my end. Thanks for introducing sort.


Reply to this email directly or view it on GitHub.

a=a+ table_init_markup
for k,v in ordered_json.iteritems():
a=a+ '<tr>'
a=a+ '<th>'+ str(k) +'</th>'
if (v==None):
v = unicode("")
v = unicode("")
if(isinstance(v,list)):
a=a+ '<td><ul>'
for i in range(0,len(v)):
if(isinstance(v[i],unicode)):
a=a+ '<li>'+unicode(v[i])+'</li>'
elif(isinstance(v[i],int) or isinstance(v,float)):
a=a+ '<li>'+str(v[i])+'</li>'
elif(isinstance(v[i],list)==False):
self.iterJson(v[i])
a=a+ '</ul></td>'
a=a+ '</tr>'
elif(isinstance(v,unicode)):
a=a+ '<td>'+ unicode(v) +'</td>'
a=a+ '</tr>'
elif(isinstance(v,int) or isinstance(v,float)):
a=a+ '<td>'+ str(v) +'</td>'
a=a+ '</tr>'
else:
a=a+ '<td>'
#a=a+ '<table border="1">'
self.iterJson(v)
a=a+ '</td></tr>'
column_headers = self.columnHeadersFromListOfDicts(v)
if column_headers != None:
a=a+ '<td>'
a=a+ table_init_markup
a=a+ '<tr><th>' + '</th><th>'.join(column_headers) + '</th></tr>'
for list_entry in v:
a=a+ '<tr><td>' + '</td><td>'.join([markup(list_entry[column_header]) for column_header in column_headers]) + '</td></tr>'
a=a+ '</table>'
a=a+ '</td>'
a=a+ '</tr>'
continue
a=a+ '<td>' + markup(v) + '</td>'
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if markup(v) return None? I got some errors regarding this.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see, could you try now?

a=a+ '</tr>'
a=a+ '</table>'
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+ '<li>'+unicode(ordered_json[i])+'</li>'
elif(isinstance(ordered_json[i],int) or isinstance(ordered_json[i],float)):
a=a+ '<li>'+str(ordered_json[i])+'</li>'
elif(isinstance(ordered_json[i],list)==False):
self.htmlConvertor(ordered_json[i])

return a

return self.iterJson(ordered_json)

json2html = JSON()
json2html = JSON()