I am trying to build a JSON object from the tree class, I just need the span values. the tree structure is dynamic and nested so i think the best approach to do that is using some recursion function. currently I just succeed to extract the key and the values but I am not succeeding to build the JSON object.
I am using jinja2 when I building the html file.
this is the html code:
<div class="tree well">
<ul id="treeList">
<li >
<span id="deviceName" class="fa fa-tablet"> {{report["name"]}}</span>
<ul>
{% for v in report["reports"] %}
<li >
<span id="{{v['_id']}}" class="fa fa-code-fork"> {{v["_id"]}} </span>
<ul>
{% for key, value in v.items() recursive %}
{% if key != '_id' %}
<li id="li_{{key}}">
{% if value is not mapping %}
{% if key == 'status' %}
<span class="status" id="{{key}}"> {{value}}</span>
{% elif key == 'issues' %}
<span class="fa fa-bug issues" id="issues"> issues</span>
{% elif key == 'updated_on' %}
<span id="updateOn" class="fa fa-calendar-o "> {{value}} </span>
{% endif %}
{% endif %}
{% if value is mapping %}
{% if key == 'attackProccess' %}
<span id="attackProccess" class="fa fa-sitemap"> {{key}} </span>
{% elif key == 'data' %}
<span id="data" class="fa fa-database"> data </span>
{% else %}
<span id="{{key}}" class="fa fa-minus-circle"> {{key}} </span>
{% endif %}
<ul id="ul-attack-items">
{{ loop(value.items())}}
</ul>
{% endif %}
</li>
{% endif %}
{% endfor %}
</ul>
</li>
{% endfor %}
</ul>
</li>
</ul>
</div>
this is the jquery part:
// empty string to save the result
var dataList = "";
// find all li elements within the element with the id list
var $entries = $("#treeList").find('span');
// iterate through all these items, with i as index and item itself as entry
$entries.each(function(i, entry) {
// append the elements id attribute and its content to the string, like: item_1=Item1 for <li id="item_1">Item1</li>
dataList += ' '+$(entry).attr('id') + ':' + $(entry).text();
// add & to the string, if the entry is not the last one
if (i != $entries.length-1) {
dataList += '';
}
dataList = dataList.replace(/(\r\n|\n|\r)/gm,"");
and this is the model i want:
{
"_id" : "aa:dd:ff:ff:cc:ac",
"account" : "iphonex@gmail.com",
"name" : "Iphone X",
"reports" : [
{
"updated_on" : "2017-11-27",
"_id" : "iOS 11.1",
"attackProccess" : {
"item1" : {
"status" : "todo"
},
"item2" : {
"status" : "todo"
},
"item3" : {
"status" : "todo"
},
"item4" : {
"status" : "todo"
},
"item5" : {
"status" : "todo"
},
"item6" : {
"status" : "todo"
}
},
"issues" : [],
"data" : {
"files" : {
"status" : "todo"
},
"chats" : {
"viber" : {
"status" : "todo"
},
"facebook" : {
"status" : "todo"
},
"instagram" : {
"status" : "todo"
},
"twitter" : {
"status" : "todo"
},
"skype" : {
"status" : "todo"
},
"telegram" : {
"status" : "todo"
},
"whatsapp" : {
"status" : "todo"
},
"line" : {
"status" : "todo"
}
},
"sms" : {
"status" : "todo"
},
"app_list" : {
"status" : "todo"
},
"telegram" : {
"status" : "todo"
},
"downloads" : {
"status" : "todo"
},
"locations" : {
"status" : "todo"
},
"catchapp" : {
"status" : "todo"
},
"bookmarks" : {
"status" : "todo"
},
"calendar" : {
"status" : "todo"
},
"contacts" : {
"status" : "todo"
},
"media" : {
"status" : "todo"
},
"notes" : {
"status" : "todo"
},
"posts" : {
"status" : "todo"
},
"call_history" : {
"status" : "todo"
}
}
}
]
}