Read & Create JSON

Convert one format of JSON to another

JavaScript

$(document).ready(function () {
  
    var jsonObj = [
        [{
            "displayName": "CostCenter",
            "value": ""
        },
        {
            "displayName": "Id",
            "value": ""
        },
        {
            "displayName": "BoxNumber",
            "value": "789654"
        }],
        [{
            "displayName": "CostCenter",
            "value": "cc1"
        },
        {
            "displayName": "Id",
            "value": "1"
        },
        {
            "displayName": "BoxNumber",
            "value": "789654"
        }],
        [{
            "displayName": "CostCenter",
            "value": "cc2"
        },
        {
            "displayName": "Id",
            "value": "2"
        },
        {
            "displayName": "BoxNumber",
            "value": "789654"
        }]
    ];
    
    
    var requiredJSONFormat = [];
    
    $(jsonObj).each(function(){
        var currentMapData = {};
        $(this).each(function(i, currentItem){
           // alert(currentItem.displayName);
            currentMapData[currentItem.displayName] = currentItem.value;
            
        });
        requiredJSONFormat.push(currentMapData);
    });
    
    alert(JSON.stringify(requiredJSONFormat));
});