Search code examples
pythonjsonpython-2.7json2html

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!


Solution

  • 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)