jQuery('#parentId').select2({ placeholder: 'Please select a parent department', allowClear: true, ajax: { url: url, dataType: 'json', delay: 250, data: function (params) { return { q: params.term, }; }, processResults: function (data) { var formatParents = []; jQuery.each(data.parents, function(index, item) { formatParents.push({id: item.department_id, text: item.parent}); }); return { results: formatParents }; }, cache: true }, minimumInputLength: 1, });