0

I am usign JSTree to show tree structure. Now I need to show selected parent/child when edit a record. For this I've below json that is required json to show selected parent/child node in tree.

varpathArray={
  "func_extensions_job": [
    {
      "CodiacSDK.NewModule.dll;extensionCreatePre;": [
        {
          "CodiacSDK.NewModule.dll;extensionInquirePost;": [

          ]
        }
      ]
    }
  ]
};

But its static json and I need dynamic json to convert like above. Below is the json that I get via API response. Now I need to add an empty array at last value.

{"func_extensions_job":[{"CodiacSDK.NewModule.dll;extensionCreatePre;":[{"CodiacSDK.NewModule.dll;extensionExecutePost;":["CodiacSDK.PythonWrapper.dll;UnitValues::extensionExecutePre;"]}]}]}

I've this

["CodiacSDK.PythonWrapper.dll;UnitValues::extensionExecutePre;"]

That I need to convert in this

[{"CodiacSDK.NewModule.dll;extensionInquirePost;":[]}]

So is there any way that I can do it.

I am using below function to generate my json array without empty object

var path = $("#testing").jstree().get_path($("#testing").jstree("get_selected", true)[0], '{/}').split('{/}');

            var resJson = {};

            console.log(JSON.stringify(resJson));

            for (var j = path.length; j > 1; j--) {
                //console.log(path[j - 2]);

                if (path[j - 2] && !Object.keys(resJson).length) {
                    resJson[path[j - 2]] = [path[j - 1]];
                } else {
                    var temp = resJson;
                    resJson = {};
                    resJson[path[j - 2]] = [temp];
                }
            }

Any help is appreciated. Thanks in advance.

8
  • Convert to an object add an empty array and then reconvert to JSON Commented Jul 20, 2018 at 10:01
  • Thanks for quick response but can you please give me the example. I've very little knowledge of javascript/jquery. Commented Jul 20, 2018 at 10:02
  • This might help stackoverflow.com/questions/2257117/json-string-to-js-object Commented Jul 20, 2018 at 10:04
  • Yes that I can do but I need to add empty array at the last item in JSON array like I've item in [item] and I need to convert it into [{item:[]}] in the same parent where it belongs. Please refer my complete JSON array in my original question for more information. Commented Jul 20, 2018 at 10:12
  • 1
    if you are getting an object then can't you simply access the internal object and assign it with an empty array ? like this stackoverflow.com/questions/931872/… won't this solve your problem of adding an empty array ? Commented Jul 20, 2018 at 12:43

0

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.