skip to Main Content

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?

2

Answers


  1. Chosen as BEST ANSWER

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

    import json
    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 = xlist[0][0]
    chapter = xlist[0][1]
    jstr = '[{"nodes":[{"nodes":['
    
    for x in xlist:
        if x[0] != title: 
            jstr = jstr[:-1] + f'],"text":"{chapter}"' + '}],' + f'"text":"{title}"' + '},{"nodes":[{"nodes":['
            title = x[0]
            chapter = x[1]
        elif x[1] != 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]}"' + '}]'
    parsed = json.loads(jstr)
    print(json.dumps(parsed,indent=2))
    

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

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search