McCourt2364
McCourt2364

Reputation: 61

JSON2HTML: Not a valid JSON list python

I have a piece of JSON in a file I would like to convert to HTML. I seen online there is a tool called json2html for python which takes care of this for me.

[{
    "name": "Steve",
    "timestampe": "2016-07-28 10:04:15",
    "age": 22
}, 
{
    "name": "Dave",
    "timestamp": "2016-07-28 10:04:15",
    "age": 34
}]

Above is my JSON, when using the online converter tool - http://json2html.varunmalhotra.xyz/ it works great and produces a nice table for me.

However when I install the library using pip and run the following:

_json = [{
    "name": "Steve",
    "timestampe": "2016-07-28 10:04:15",
    "age": 22
}, 
{
    "name": "Dave",
    "timestamp": "2016-07-28 10:04:15",
    "age": 34
}]

print json2html.convert(json=_json)

I get an error

 File "/root/.pyenv/versions/venv/lib/python2.7/site-packages/json2html/jsonconv.py", line 162, in iterJson
raise Exception('Not a valid JSON list')
 Exception: Not a valid JSON list

I even ran the json through http://jsonlint.com/ and it came back as valid JSON.

I was wondering if anyone would have a fix for this, or could point me in the right direction on how to solve this. I can't find much documentation on this library.

For reference this is the link to the pypi library - https://pypi.python.org/pypi/json2html

Any help would be appreciated, thanks in advance!

Upvotes: 0

Views: 1012

Answers (2)

Mohammad Esmaeilbeygi
Mohammad Esmaeilbeygi

Reputation: 84

parameter json must be a dictionary object and you pass a list. try this:

_json = { "data" : [{"name": "Steve",
    "timestampe": "2016-07-28 10:04:15",
    "age": 22
}, 
{
    "name": "Dave",
    "timestamp": "2016-07-28 10:04:15",
    "age": 34
}]
}
print json2html.convert(json=_json)

Upvotes: 1

Brae
Brae

Reputation: 514

Try setting the value of _json using json.loads(), as in this answer - Converting json to html table in python

Upvotes: 0

Related Questions