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.

Community
  • 1
  • 1

2 Answers2

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'>.

gus27
  • 2,616
  • 1
  • 21
  • 25
  • 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), https://github.com/softvar/json2html/pull/2" in jsonconv.py. Do you see anything wrong with that? – Sabrina Lautier Oct 04 '16 at 07:20
  • @Sabrina Lautier: This seems absolutely correct. What platform are you using (Windows, Linux, ...) and do you know the encoding of your python file? – gus27 Oct 04 '16 at 07:35
  • also, I use Python v2.6:`$ python --version Python 2.7.12` – Sabrina Lautier Oct 04 '16 at 07:50
  • I use Window7 and Eclipse (Mars). The encoding of the Python file is by default **Cp1252**: Default (inherited from container: CP1252). – Sabrina Lautier Oct 04 '16 at 07:54
  • 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): 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`. – Sabrina Lautier Oct 04 '16 at 08:30
  • Is the small "complete sample" from my answer running on your site and what does it output if you run it? – gus27 Oct 04 '16 at 08:33
  • In short, if I execute the code `for j in json_obj:...` with the json object initialized directly `json_obj = [{"Agent Status": "status1", ... }]`everything is fine. The same code does not work when I construct the JSON array: in that case, j takes every single character of the json data: `'[' '{' '"' 'A'`. This is weird as I create the json array using `json.dumps(anomaly_list, sort_keys=True)`, where anomaly_list is a list of dictionary. Would you have any clue? – Sabrina Lautier Oct 04 '16 at 08:44
  • Yes the "complete sample" from your answer is running fine as long as I initialize the json array manually (**with the output provided by my script**, just copy/paste). If I initialize it using `json.dumps(anomaly_list, sort_keys=True)`, then it does not work anymore, as `j`takes every single character of the json data: `'[' '{' '"' 'A'`. Really weird... – Sabrina Lautier Oct 04 '16 at 08:49
  • No, that is not weird - you only forget to mention the `json.dumps` statement in your question. `json.dumps` does the following: "Serialize obj to a JSON formatted str". So `json_obj` is a `str` (a string) which you put into the parameter of your `json2html.convert` call. It is not an array of objects. Try to get rid of `json.dumps`. – gus27 Oct 04 '16 at 11:09
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.

  • It would be better to insert your answer/clarification at the bottom of your question with a preceding **UPDATE** text. – gus27 Oct 04 '16 at 14:55
  • 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. – gus27 Oct 05 '16 at 08:15