0

I have a jquery third party application that has nested lists that serialize to an output like below. The list will always only have 2 levels, but I am having trouble trying to figure out how to parse it. I am using coldFusion.

The List Looks like (line breaks added for visualization, they cannot be used as a delimiter):

[{"id":1},
{"id":197,"children":[{"id":198},{"id":199},{"id":200}]},
{"id":2,"children":[{"id":3},{"id":4},{"id":143},{"id":6},{"id":5},{"id":7},{"id":8},{"id":9},{"id":10},{"id":11},{"id":12}]},
{"id":15,"children":[{"id":17},{"id":190},{"id":191},{"id":131},{"id":16},{"id":142},{"id":124}]},
{"id":114}]

I want to loop through each id and convert into a parentid and childid like so:

id:1   parentid: 10000 childid: 10000
id:197 parentid: 10001 childid: 10000 (new parent)
id:198 parentid: 10001 childid: 10001 (first child)
id:199 parentid: 10001 childid: 10002 (second child)
id:200 parentid: 10001 childid: 10003 (third child)
id:2   parentid: 10002 childid: 10000 (new parent)

... and so on

Your help is appreciated.


Edit: Code is below for what I am trying to do

<script type="text/javascript">
$(document).ready(
    function()
    {
    var updateOutput = function(e)
    {
        var list   = e.length ? e : $(e.target),
            output = list.data('output');
        if (window.JSON) {
            output.val(window.JSON.stringify(list.nestable('serialize')));//, null, 2));
        } else {
            output.val('JSON browser support required for this demo.');
        }
    };

//this is where i need help     
var postOutline = function(output){
        $.post("something.cfc", {
        method: 'getoutline',
        output: output
        }); 
    };

    // activate Nestable for list 1
    $('#nestable3').nestable({
        group: 1
    })
   // .on('change', updateOutput);
    .on('change', postOutline);

    // output initial serialised data
    updateOutput($('#nestable3').data('output', $('#nestable-output')));



    }
);
</script>
4
  • Just an observation. In that data, NONE of the IDs mentioned in the CHILDREN arrays refer to IDs of the actual elements. They're all orphans. Is that correct? Is this just dummy data? Commented Mar 29, 2013 at 3:20
  • not sure what you mean by orphans... but all of the children are separate page ids for a webpage in the database. its an outline for a CMS i am trying to create. Commented Mar 29, 2013 at 3:26
  • You have three "ID types": ID, PARENTID and CHILDID. You have the relationship between ID and CHILDID in your data. But none of the IDs have PARENTS. You only have IDs and CHILDREN. Basically you're after three "generations" of relationships, but your data only reflects two generations. Make sense? Commented Mar 29, 2013 at 3:33
  • the id is the parentid except i need parentid to be in sequential order so the outline stays intact - i guess there are better ways to do this but this is the structure I came up with Commented Mar 29, 2013 at 3:37

1 Answer 1

1

You just need to use deserializeJson(). You don't need to parse it by hand. From the docs:

Description Converts a JSON (JavaScript Object Notation) string data representation into CFML data, such as a CFML structure or array.

From there you simply need to use your usual CFML to process it however you like.

Finding the children for each ID is easy as it's in the data structure.

Unfortunately the data structure is not ideal for extracting parent information.

The most expedient way I can think of is to use structFindValue() to find all the occurrences of the current ID, and then loop over that finding the entry for which the match has a descendant children". Then traverse across to the ID, which will be the parent ID of those children (if that makes sense).

(the above initial suggestion won't work, as structFindValue() doesn't give enough information).

You're gonna need to brute-force this, doing something like this:

array = deserializeJson(json); // json is your data from the client

for (childElement in array){
    childId = childElement.id;
    for (parentElement in array){
        if (structKeyExists(parentElement, "children")){ // not all of them have children
            if (arrayFind(parentElement.children, {id=childId})){
                writeOutput("Parent of #childId# is #parentElement.id#<br>");
            }
        }
    }
}

Obviously that's not a precise solution for what you need, but it shows the technique for looking-up the parent. Someone else might be able to come up with a less ham-fisted way of doing it.

Sign up to request clarification or add additional context in comments.

3 Comments

wow, that looks easy. Thanks. However, how can I get the output from a jquery variable and store it in a coldfusion variable? The output is generated like so: var updateOutput = function(e) { var list = e.length ? e : $(e.target), output = list.data('output'); if (window.JSON) { output.val(window.JSON.stringify(list.nestable('serialize')));//, null, 2)); } else { output.val('JSON browser support required for this demo.'); } };
Oh right. Well you've got to get it back to the ColdFusion server first. Bear in mind that JQuery (Javascript) runs on the client browser, which is completely disconnected from the ColdFusion server. Sorry, I was presuming you already had the JSON string back on the CF end of things. You'll need to post it back to the CF server via some mechanism. If you don't know how to do that, that's another question entirely (so ask it, and we can deal with that too).
Yeah I just need a function to post the data back to a cfc to deal with it from there and write a function to parse the output as you suggested. I've edited my question above but am getting an error with my function. Thanks for your help so far.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.