Search code examples
pythonjson2html

json2html not outputting as described, want to remove generated outer table


I'm using json2html (python) and the outputs are not matching what is shown at the website. https://pypi.org/project/json2html/

Basically, using the site example:

from json2html import *

input = {
    "sample": [{
        "a": 1, "b": 2, "c": 3
    }, {
        "a": 5, "b": 6, "c": 7
    }]}

json2html.convert(json=input)

Shows the output on the website as a table of 2 rows.

enter image description here

However, the html created and embedded on the same page now outputs this (and is also what the code now says it is doing), just ignore the table formatting that is just my css styling:

enter image description here

If I remove the 'sample' element name it produces two separate tables and clubbing True/False just makes it worse.

Can anyone let me know how to get it to show the version the website describes it as the left-sided name is pretty ugly and redundant. Thanks.


Solution

  • To achieve that, remove the sample and pass a list of two rows like this:

    from json2html import *
    
    inp = [
        {
            "a": 1, "b": 2, "c": 3  # row 1
        },
        {
            "a": 5, "b": 6, "c": 7  # row 2
        }
    ]
    json2html.convert(json=inp)
    

    Result:

    A table with 3 rows and 3 columns