0

I have written a function which scans all the files/directory in a given directory and returns a json object. I need to display this json object on the UI using jstree. I have written the function in go using recursion to scan all the files and directories rooted at that particular folder.

This is the type I am using to construct the whole structure

type Directory struct {
    Name string "data"
    SubDirs []Directory "children"
}

Now jstree accepts data structs of the following format.

json_data: {
        data: [
            "f1",
            "f2",
            {
                data: "f3",
                children: ["f4", "f5"]
            }
        ]
 }

and not of the format :-

json_data: {
        data: [
            {
              "data": "f1",
              "children": []
            } 
            {
              "data": "f2",
              "children": []
            } 
            {
                data: "f3",
                children: ["f4", "f5"]
            }
        ]
 }

(when I pass the above data structure format, it doesn't work probably since the "children" thing is null)

Thus the data structure Directory I have maintained doesn't suffice to construct the directory structure.

How do I solve this problem of constructing the Directory tree homogeneously?

1 Answer 1

1

The final one is not even valid JSON. The spec defines that empty arrays are valid and allowed. Try:

{
    "data": [
        {
            "data": "f1",
            "children": []
        },
        {
            "data": "f2",
            "children": []
        },
        {
            "data": "f3",
            "children": [
                "f4",
                "f5"
            ]
        }
    ]
}

Use JSONLint to validate your JSON objects.

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

2 Comments

I guess the main problem is of jstree, how do I solve in jstree's perspective because it says that it accepts json objects but isn't accepting the above which is a valid json object
@chinmay: I also think that it makes sense to be able to represent empty directories as a directory with no children, different from a file. and if jstree doesn't support that, then it seems like an issue with jstree

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.