2

I'm trying to format json data to html using json2html. The json data look like this:

json_obj = [{"Agent Status": "status1", "Last backup": "", "hostId": 1234567, "hostfqdn": "test1.example.com", "username": "user1"}, {"Agent Status": "status2", "Last backup": "", "hostId": 2345678, "hostfqdn": "test2.example.com", "username": "user2"}]

As already reported in post "json2html not a valid json list python", to make the code works, the json parameter must be a dictionary and not a list, so I'm calling it that way:

json_obj_in_html = json2html.convert(json = { "data" : json_obj })

However it does not format the json data to html only the first level of dictionary { "data" : json_obj }:

print json_obj_in_html
<table border="1"><tr><th>data</th><td>[{"Agent Status": "status1", "Last backup": "", "hostId": 1234567, "hostfqdn": "test1.example.com", "username": "user1"}, {"Agent Status": "status2", "Last backup": "", "hostId": 2345678, "hostfqdn": "test2.example.com", "username": "user2"}]</td></tr></table>

Note that the online convert tool provides the right output: http://json2html.varunmalhotra.xyz/

<table border="1"><tr><th>data</th><td><ul><table border="1"><tr><th>Agent Status</th><td>status1</td></tr><tr><th>Last backup</th><td></td></tr><tr><th>hostId</th><td>1234567</td></tr><tr><th>hostfqdn</th><td>test1.example.com</td></tr><tr><th>username</th><td>user1</td></tr></table><table border="1"><tr><th>Agent Status</th><td>status2</td></tr><tr><th>Last backup</th><td></td></tr><tr><th>hostId</th><td>2345678</td></tr><tr><th>hostfqdn</th><td>test2.example.com</td></tr><tr><th>username</th><td>user2</td></tr></table></ul></td></tr></table>

Any help would be very welcome.

0

2 Answers 2

2

Make sure that json_obj is an array of objects and not a string (str).

I put your code to a complete sample:

from json2html import *
json_obj = [{"Agent Status": "status1", "Last backup": "", "hostId": 1234567, "hostfqdn": "test1.example.com", "username": "user1"}, {"Agent Status": "status2", "Last backup": "", "hostId": 2345678, "hostfqdn": "test2.example.com", "username": "user2"}]
json_obj_in_html = json2html.convert(json = { "data" : json_obj })
print json_obj_in_html

With Python 2.7 and json2html 1.0.1 this leads to this result: enter image description here

If you receive a result like

<table border="1"><tr><th>data</th><td>[{"Agent Status": "sta...

it is very likely that json_obj is a str and not an array of objects. You can check this by inserting a statement like

print type(json_obj)

before jsonhtml.convert. I assume that type(json_obj) returns a <type 'str'> in your case and that is why the JSON like string appears in your html. To get it right you have to modify your code in that way that type(json_obj) returns <type 'list'>.

Sign up to request clarification or add additional context in comments.

9 Comments

Thanks for your feedback. Regarding the version, I installed json2html as described in the documentation: $ pip install json2html. And when I look at the version of the files, I see: __version__ = '1.0.1' in init.py and "1. Michel Müller(@muellermichel), github.com/softvar/json2html/pull/2" in jsonconv.py. Do you see anything wrong with that?
@Sabrina Lautier: This seems absolutely correct. What platform are you using (Windows, Linux, ...) and do you know the encoding of your python file?
also, I use Python v2.6:$ python --version Python 2.7.12
I use Window7 and Eclipse (Mars). The encoding of the Python file is by default Cp1252: Default (inherited from container: CP1252).
However, this code in my file does not work: json_obj = json.dumps(anomaly_list, sort_keys=True) json_obj_in_html = '' for j in json_obj: pp(j) json_obj_in_html += json2html.convert(json = j) The output is: '[' Traceback (most recent call last): <snip> json_obj_in_html += json2html.convert(json = j) File "C:\...\json2html\jsonconv.py", line 56, in convert return self.iterJson(inputtedJson) File "C:\...\json2html\jsonconv.py", line 162, in iterJson raise Exception('Not a valid JSON list') Exception: Not a valid JSON list.
|
0

My list of dictionaries anomaly_list was already in a json format, so trying to convert it using json.dumps(anomaly_list, sort_keys=True) was turning into a string, which was not what I wanted. I solved the issue by leaving my list of dictionaries as it is and this code now works:

json_obj_in_html = ''
for j in anomalies_list:
    json_obj_in_html += json2html.convert(json = j)

It outputs what I wanted.

@gus42: thanks, your feedback made me understand where the real pb was.

2 Comments

It would be better to insert your answer/clarification at the bottom of your question with a preceding UPDATE text.
You can easily thank someone by accepting or upvoting his/her answer ;-) You may have noticed that I updated my answer to match for the str/json.dumps problem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.