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
finally, I manage to solve it, below is the code:
Converting DataFrame to JSON you can see in this Documentation
This is the example from the documentation
and the result :
If the data from list you can do something like this
The result:
I hope this answer helping solve your problem