Home > Net >  How to generate a treeview Json from a List or Dataframe in Python?
How to generate a treeview Json from a List or Dataframe in Python?

Time:01-14

I have the following List/Dataframe:

[['title1','title1_chapter1','title1_chapter1_section1','title1_chapter1_href1'], ['title1','title1_chapter1','title1_chapter1_section2','title1_chapter1_href2'], ['title1','title1_chapter2','title1_chapter2_section1','title1_chapter2_href1'], ['title1','title1_chapter2','title1_chapter2_section2','title1_chapter2_href2'], ['title2','title2_chapter1','title2_chapter1_section1','title2_chapter1_href1']]

I want to transform it to the following nested JSON such that it can be used in bootstrap treeview.

[
  {
    "nodes": [
      {
        "nodes": [
          {
            "text": "title1_chapter1_section1",
            "href": "title1_chapter1_href1"
          },{
            "text": "title1_chapter1_section2",
            "href": "title1_chapter1_href2"
          }],
        "text": "title1_chapter1"
      },{
        "nodes": [
          {
            "text": "title1_chapter2_Section1",
            "href": "title1_chapter2_href1"
          },{
            "text": "title1_chapter2_section2",
            "href": "title1_chapter2_href2"
          }],
        "text": "title1_chapter2"
      }],
    "text": "title1"
  },{
    "nodes": [
      {
        "nodes": [
          {
            "text": "title2_chapter1_section1",
            "href": "title2_chapter1_href2"
          }],
        "text": "title2_chapter1"
      }],
    "text": "title2"
  }
]

How can I do it in Python?

CodePudding user response:

Converting DataFrame to JSON you can see in this Documentation
This is the example from the documentation

import json
df = pd.DataFrame(
    [["a", "b"], ["c", "d"]],
    index=["row 1", "row 2"],
    columns=["col 1", "col 2"],
)
result = df.to_json(orient="split")
parsed = json.loads(result)
json.dumps(parsed, indent=4)  

and the result :

{
    "columns": [
        "col 1",
        "col 2"
    ],
    "index": [
        "row 1",
        "row 2"
    ],
    "data": [
        [
            "a",
            "b"
        ],
        [
            "c",
            "d"
        ]
    ]
}

If the data from list you can do something like this

import json

aList = [{'a':1, 'b':2}, {'c':3, 'd':4}]
jsonStr = json.dumps(aList, indent=4)
print(jsonStr)

The result:

[
    {
        "a": 1,
        "b": 2
    },
    {
        "c": 3,
        "d": 4
    }
]

I hope this answer helping solve your problem

CodePudding user response:

finally, I manage to solve it, below is the code:

xlist = [['title1','title1_chapter1','title1_chapter1_section1','title1_chapter1_href1'],
         ['title1','title1_chapter1','title1_chapter1_section2','title1_chapter1_href2'],
         ['title1','title1_chapter2','title1_chapter2_section1','title1_chapter2_href1'],
         ['title1','title1_chapter2','title1_chapter2_section2','title1_chapter2_href2'],
         ['title2','title2_chapter1','title2_chapter1_section1','title2_chapter1_href1']]

title = ''
chapter = ''
jstr = '[{"nodes":[{"nodes":['

for x in xlist:
    if x[0] != title: 
        if chapter !='':
            jstr = jstr[:-1]   f'],"text":"{chapter}"'   '},'
        if title != '': 
            jstr = jstr[:-1]   f'],"text":"{title}"'   '},{"nodes":[{"nodes":['
        title = x[0]
    elif x[1] != chapter: 
        if chapter != '':
            jstr = jstr[:-1]   f'],"text":"{chapter}"'   '},{"nodes":['
        chapter = x[1]
    jstr = jstr   '{'   f'"text":"{x[2]}","href":"{x[3]}"'   '},' 
        
jstr = jstr[:-1]   f'],"text":"{x[1]}"'   '}],'   f'"text":"{x[0]}"'   '}]'
            
print(jstr)
  • Related