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.

Tejal
  • 764
  • 1
  • 10
  • 39
user2935236
  • 189
  • 2
  • 11
  • Convert to an object add an empty array and then reconvert to JSON – Bilbo Baggins Jul 20 '18 at 10:01
  • Thanks for quick response but can you please give me the example. I've very little knowledge of javascript/jquery. – user2935236 Jul 20 '18 at 10:02
  • This might help https://stackoverflow.com/questions/2257117/json-string-to-js-object – Bilbo Baggins Jul 20 '18 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. – user2935236 Jul 20 '18 at 10:12
  • if you can create a Javascript object then first do that and then add the [] at the place you want. and then again convert it to JSON. – Bilbo Baggins Jul 20 '18 at 10:15
  • I think I am not able to clear my doubt. The response I've in an object only I've just converted it to string to understand better. If I run loop for an object then it's only one object but its nested with multiple levels, like first, then second in first, third in second, fourth in third. Now I need to add/convert the fourth item in nested level and that I am not able to do it. – user2935236 Jul 20 '18 at 10:22
  • 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 https://stackoverflow.com/questions/931872/what-s-the-difference-between-array-and-while-declaring-a-javascript-ar won't this solve your problem of adding an empty array ? – Bilbo Baggins Jul 20 '18 at 12:43
  • Yes really helped, I've done it thanks – user2935236 Jul 20 '18 at 14:16

0 Answers0