Ganesh sah Ganesh sah - 6 months ago 30
Javascript Question

i need to convert array value to json in jquery

i have array value. need to convert this array value into json format. example is given bleow

Sample Array

[Management Portal!@!@Production Issue Handling!@!@/IONSWeb/refDataManagement/searchDynamicScripts.do, Management Portal!@!@ Event Browser!@!@/IONSWeb/orderManagement/eventBrowser.do, Management Portal!@!@ Order Workflow!@!@/IONSWeb/orderManagement/SearchOrdersWorkflow.do, ADMINISTRATION!@!@Admin Message!@!@/IONSWeb/userManagement/getMessageForBroadcast.do, ADMINISTRATION!@!@Audit!@!@/IONSWeb/userManagement/auditManagement.do, ADMINISTRATION!@!@Locks!@!@/IONSWeb/userManagement/lockSearch.do, ADMINISTRATION!@!@Queue!@!@/IONSWeb/GroupManagement/begin.do, ADMINISTRATION!@!@Role!@!@/IONSWeb/userManagement/goToRolePage.do, ADMINISTRATION!@!@Routing Rule!@!@/IONSWeb/ruleManagement/showRules.do, ADMINISTRATION!@!@Task Code!@!@/IONSWeb/ManageTaskCode/begin.do, ADMINISTRATION!@!@Trigger OutEvent!@!@/IONSWeb/triggerOutEvent.jsp, ADMINISTRATION!@!@User!@!@/IONSWeb/userManagement/begin.do, ADMINISTRATION!@!@Refresh Application Cache!@!@/IONSWeb/userManagement/refreshApplnCache.do]


sample Json

{
"name": "Administration",
"sub": [
{
"name": "Add Order",
"url": "/IONSWeb/userManagement/auditManagement.do"
},
{
"name": "Infrastructure sonet Add Order ",
"url": "/IONSWeb/userManagement/auditManagement.do"
},
{
"name": "fGNS Add Order",
"url": "/IONSWeb/userManagement/auditManagement.do"
}
]
}


Please anyone help on this

mhu mhu
Answer

Try something like this:

Fiddle: https://jsfiddle.net/ug85d7o7/6/

var jsonData = [], 
    item, name,
    subItem, subUrl,
    i, j,
    a = [
        "Management Portal!@!@Production Issue Handling!@!@/IONSWeb/refDataManagement/searchDynamicScripts.do", 
        "Management Portal!@!@ Event Browser!@!@/IONSWeb/orderManagement/eventBrowser.do",
        "Management Portal!@!@ Order Workflow!@!@/IONSWeb/orderManagement/SearchOrdersWorkflow.do", 
        "ADMINISTRATION!@!@Admin Message!@!@/IONSWeb/userManagement/getMessageForBroadcast.do", 
        "ADMINISTRATION!@!@Audit!@!@/IONSWeb/userManagement/auditManagement.do",
        "ADMINISTRATION!@!@Locks!@!@/IONSWeb/userManagement/lockSearch.do",
        "ADMINISTRATION!@!@Queue!@!@/IONSWeb/GroupManagement/begin.do", 
        "ADMINISTRATION!@!@Role!@!@/IONSWeb/userManagement/goToRolePage.do", 
        "ADMINISTRATION!@!@Routing Rule!@!@/IONSWeb/ruleManagement/showRules.do", 
        "ADMINISTRATION!@!@Task Code!@!@/IONSWeb/ManageTaskCode/begin.do", 
        "ADMINISTRATION!@!@Trigger OutEvent!@!@/IONSWeb/triggerOutEvent.jsp",
        "ADMINISTRATION!@!@User!@!@/IONSWeb/userManagement/begin.do",
        "ADMINISTRATION!@!@Refresh Application Cache!@!@/IONSWeb/userManagement/refreshApplnCache.do"
    ];

for(i=0; i<a.length; i++)
{
    item = a[i].split("!@!@");
    name = item[0];
    subName = item[1];
    subUrl = item[2];

    subItem = null;
    for (j=0; j<jsonData.length; j++)
    {
        if (jsonData[j].Name == name)
        {
            subItem = jsonData[j].sub;
            break;
        }
    }

    if (!subItem)
    {
        jsonData.push({"Name" : name, "sub" : [] });
        subItem = jsonData[jsonData.length-1].sub;        
    }

    subItem.push({"Name" : subName, "url" : subUrl });
}

alert(JSON.stringify(jsonData));

Result:

[
  {
    "Name": "Management Portal",
    "sub": [
      {
        "Name": "Production Issue Handling",
        "url": "/IONSWeb/refDataManagement/searchDynamicScripts.do"
      },
      {
        "Name": " Event Browser",
        "url": "/IONSWeb/orderManagement/eventBrowser.do"
      },
      {
        "Name": " Order Workflow",
        "url": "/IONSWeb/orderManagement/SearchOrdersWorkflow.do"
      }
    ]
  },
  {
    "Name": "ADMINISTRATION",
    "sub": [
      {
        "Name": "Admin Message",
        "url": "/IONSWeb/userManagement/getMessageForBroadcast.do"
      },
      {
        "Name": "Audit",
        "url": "/IONSWeb/userManagement/auditManagement.do"
      },
      {
        "Name": "Locks",
        "url": "/IONSWeb/userManagement/lockSearch.do"
      },
      {
        "Name": "Queue",
        "url": "/IONSWeb/GroupManagement/begin.do"
      },
      {
        "Name": "Role",
        "url": "/IONSWeb/userManagement/goToRolePage.do"
      },
      {
        "Name": "Routing Rule",
        "url": "/IONSWeb/ruleManagement/showRules.do"
      },
      {
        "Name": "Task Code",
        "url": "/IONSWeb/ManageTaskCode/begin.do"
      },
      {
        "Name": "Trigger OutEvent",
        "url": "/IONSWeb/triggerOutEvent.jsp"
      },
      {
        "Name": "User",
        "url": "/IONSWeb/userManagement/begin.do"
      },
      {
        "Name": "Refresh Application Cache",
        "url": "/IONSWeb/userManagement/refreshApplnCache.do"
      }
    ]
  }
]
Comments