Service Virtualization

 View Only

How can we sort a JSON object alphabetically using javascript

  • 1.  How can we sort a JSON object alphabetically using javascript

    Posted Mar 13, 2017 01:02 PM

    I have a scenario where my virtual service Listener step is interpreting a JSON message. This JSON contains objects which are not in alphabetical order and my challenge is to sort the JSON alphabetically.

    Example of JSON:-

     

    {
     "id": "0001",
     "type": "donut",
     "name": "Cake",
     "ppu": 0.55,
     "batters":
     {
     "topping":
     [
     { "id": "5001", "type": "None" },
     { "id": "5002", "type": "Glazed" },
     { "id": "5005", "type": "Sugar" },
     { "id": "5007", "type": "Powdered Sugar" },
     { "id": "5006", "type": "Chocolate with Sprinkles" },
     { "id": "5003", "type": "Chocolate" },
     { "id": "5004", "type": "Maple" }
     ]
     },
     "batter":
     [
     { "id": "1001", "type": "Regular" },
     { "id": "1002", "type": "Chocolate" },
     { "id": "1003", "type": "Blueberry" },
     { "id": "1004", "type": "Devil's Food" }
     ]
    }

     

    I need to sort the above nested Objects alphabetically like example :- batter --> topping

     

    If anybody have implemented this before or having any ideas how can we achieve this are requested to share there views and ideas.

     

    I am reachable at --> AdilUmer.Akhtar@techmahindra.com

     

     

     

    Regards,

    Adil