This question has already been answered:
There is an array in json format, while it is in the code, everything works. If I put it into a separate json file and in the code I call it into a variable, then the output in html does not work.
{ id: 0, title: "root - not displayed", children: [{ id: 0, title: "Россия", children: [{ id: 0, title: "Архангельская обл", children: [{ id: 111, title: "Архангельск", }, { id: 112, title: "Северодвинск", }] }, { id: 12, title: "Москва", }] }, { id: 0, title: "Украина", children: [{ id: 21, title: "Киев", }, { id: 22, title: "Одесса", }] }, { id: 0, title: "Белоруссия", children: [{ id: 31, title: "Минск", }, { id: 32, title: "Гродно", }] }] } so i get json into variable
var tree = $.get('cityTreeExample.json', function (data) { tree = data;}); I deduced in html an array so
function addCityTree(parentUL, branch) { for (var key in branch.children) { var item = branch.children[key]; $item = $('<li>', { id: "item" + item.id, class: 'list-tree__sub-item' }); if (item.id==0) { $item.append($('<div>', { class: 'toogle-accordion', text: item.title })); } else { $item.append($('<input>', { type: "checkbox", id: item.id, value: item.title })); $item.append($('<label>', { for: item.id, text: item.title, })); $item.wrapInner($('<div>', { class: 'checkbox' })); } parentUL.append($item); if (item.children) { var $ul = $('<ul>', { class: 'list-tree__sub-list' } ).appendTo($item); $item.append(); addCityTree($ul, item); } } } //call function, which builds tree from json addCityTree($('.js-city-tree'), tree); $(':checkbox').each(function () { $(this).find(':checkbox'); var matchingId = $(this).attr('id'); ($(this).attr('checked')) $('input[id*=' + matchingId +']').each(function() { $(this).removeAttr('checked'); $(this).prop('checked', $(this).attr('checked')); }); }); $('.toogle-accordion').click(function(){ $(this).closest('li').children('ul').slideToggle(); }); // add span tag to label $('.list-tree__sub-item label').prepend('<span></span>'); 
jsonvariable before thefunction (data) { json = data;}executed. and / or 2. You mistakenly believe that thetreevariable contains your data. - Igor$.get: api.jquery.com/jquery.get - Igor