Extract JSON fields in javascript to a variable

I am trying to use ajax to push and post information to Django. In my model, I only use three fields: id, parent,and text.

When I try to post the information back to Django, I have an error because the JSON file has extra fields with information that I don't have in my model.

I want to be able to collect only the fields id, parent, and text from the JSON data that I have so that I can post it successfully back to Django.

Here is my current code:

    $('#jstree_demo').jstree({
            "core" : {
                "animation" : 0,
                "check_callback" : true,
                "themes" : { "stripes" : true },
                'data' : {
                  "url" : "/snippets/",
                  "success": function(data){
                     series = data;
            },
                  "dataType" : "json" // needed only if you do not supply JSON headers
                }
            },

            "plugins" : [ "contextmenu", "dnd", "search", "state", "types", "wholerow" ]
        });
});


function get_tree(){

    var v = $('#jstree_demo').jstree(true).get_json('#', {flat:true})
    var mytext = JSON.stringify(v);

    $.ajax({
      url:/snippets/,
      type:"POST",
      data:mytext,
      contentType:"application/json; charset=utf-8",
      dataType:"json",

    })

}


function demo_create() {
    var ref = $('#jstree_demo').jstree(true),
        sel = ref.get_selected();
    if(!sel.length) { return false; }
    sel = sel[0];
    sel = ref.create_node(sel, {"type":"default"});
    if(sel) {
        ref.edit(sel);
    }

    get_tree();
};
function demo_rename() {
    var ref = $('#jstree_demo').jstree(true),
        sel = ref.get_selected();
    if(!sel.length) { return false; }
    sel = sel[0];
    ref.edit(sel);

    get_tree();
};
function demo_delete() {
    var ref = $('#jstree_demo').jstree(true),
        sel = ref.get_selected();
    if(!sel.length) { return false; }
    ref.delete_node(sel);

    get_tree();
};

And here is example JSON I get from variable "mytext"

[{"id":"node_one","text":"say its so","icon":true,"li_attr":{"id":"node_one"},"a_attr":{"href":"#","id":"node_one_anchor"},"state":{"loaded":true,"opened":true,"selected":false,"disabled":false},"data":{},"parent":"#","type":"default"},{"id":"some","text":"Ivan is sleepy","icon":true,"li_attr":{"id":"some"},"a_attr":{"href":"#","id":"some_anchor"},"state":{"loaded":true,"opened":false,"selected":false,"disabled":false},"data":{},"parent":"node_one","type":"default"},{"id":"node_1","text":"Node","icon":true,"li_attr":{"id":"node_1"},"a_attr":{"href":"#","id":"node_1_anchor"},"state":{"loaded":true,"opened":false,"selected":true,"disabled":false},"data":{},"parent":"#","type":"default"},{"id":"j1_10","text":"New node","icon":true,"li_attr":{"id":"j1_10"},"a_attr":{"href":"#"},"state":{"loaded":true,"opened":false,"selected":false,"disabled":false},"data":{},"parent":"node_1","type":"default"},{"id":"node1","text":"test worked","icon":true,"li_attr":{"id":"node1"},"a_attr":{"href":"#","id":"node1_anchor"},"state":{"loaded":true,"opened":false,"selected":false,"disabled":false},"data":{},"parent":"#","type":"default"},{"id":"j1_1","text":"New node","icon":true,"li_attr":{"id":"j1_1"},"a_attr":{"href":"#"},"state":{"loaded":true,"opened":false,"selected":false,"disabled":false},"data":{},"parent":"node1","type":"default"},{"id":"node2","text":"test worked 2","icon":true,"li_attr":{"id":"node2"},"a_attr":{"href":"#","id":"node2_anchor"},"state":{"loaded":true,"opened":false,"selected":false,"disabled":false},"data":{},"parent":"#","type":"default"},{"id":"j1_9","text":"New node","icon":true,"li_attr":{"id":"j1_9"},"a_attr":{"href":"#"},"state":{"loaded":true,"opened":false,"selected":false,"disabled":false},"data":{},"parent":"node2","type":"default"},{"id":"node3","text":"test worked yay","icon":true,"li_attr":{"id":"node3"},"a_attr":{"href":"#","id":"node3_anchor"},"state":{"loaded":true,"opened":false,"selected":false,"disabled":false},"data":{},"parent":"#","type":"default"},{"id":"j1_2","text":"New node","icon":true,"li_attr":{"id":"j1_2"},"a_attr":{"href":"#"},"state":{"loaded":true,"opened":false,"selected":false,"disabled":false},"data":{},"parent":"node3","type":"default"},{"id":"j1_7","text":"New node","icon":true,"li_attr":{"id":"j1_7"},"a_attr":{"href":"#"},"state":{"loaded":true,"opened":false,"selected":false,"disabled":false},"data":{},"parent":"j1_2","type":"default"},{"id":"j1_8","text":"New node","icon":true,"li_attr":{"id":"j1_8"},"a_attr":{"href":"#"},"state":{"loaded":true,"opened":false,"selected":false,"disabled":false},"data":{},"parent":"j1_2","type":"default"},{"id":"j1_3","text":"New node","icon":true,"li_attr":{"id":"j1_3"},"a_attr":{"href":"#"},"state":{"loaded":true,"opened":true,"selected":false,"disabled":false},"data":{},"parent":"node3","type":"default"},{"id":"j1_4","text":"New node","icon":true,"li_attr":{"id":"j1_4"},"a_attr":{"href":"#"},"state":{"loaded":true,"opened":false,"selected":false,"disabled":false},"data":{},"parent":"j1_3","type":"default"},{"id":"j1_6","text":"New node","icon":true,"li_attr":{"id":"j1_6"},"a_attr":{"href":"#"},"state":{"loaded":true,"opened":false,"selected":false,"disabled":false},"data":{},"parent":"j1_4","type":"default"},{"id":"j1_5","text":"New node","icon":true,"li_attr":{"id":"j1_5"},"a_attr":{"href":"#"},"state":{"loaded":true,"opened":false,"selected":false,"disabled":false},"data":{},"parent":"j1_3","type":"default"}]

I am trying to do this in coordination with jsTree. Any help is much appreciated.

Answers 1

  • Use var mytext = JSON.parse(v); instead of stringify.

    It will return Json array object. Now you can take id, parent,and text from the array using each to a temperory variable.

    Now stringfy this new variable and pass data.

    Hope this will help you to fix your problem.


Related Articles