-1

I am new to json. I am having a little trouble inconverting a json from one format to another format. This is my json to be converted.

{
    "message": "Successfully Advisor data has been fetched.",
    "success": true,
    "data": {
        "dataHeaders": ["Carrier","B2B", "EM"],    
        "dataArray": {
            "DHL": ["DHL", "45", "5","5"],
            "Fedex": ["Fedex","40", "7","6"],
            "UPS" : ["UPS","30", "10","3"],
            "TNT" : ["TNT","30", "10","3"],
        }
    }
}

I require the json to be converted to this format.

[ 
 {
  "key":"Carrier",
  "values":[
     {
        "x":"DHL",
        "y":45
     },
     {
        "x":"Fedex",
        "y":40
     },
     {
        "x":"UPS",
        "y":30
     },
     {
        "x":"TNT",
        "y":35
     }
  ]
},
{

  "key":"B2B",
  "values":[
     {
        "x":"DHL",
        "y":5
     },
     {
        "x":"Fedex",
        "y":7
     },
     {
        "x":"UPS",
        "y":10
     },
     {
        "x":"TNT",
        "y":5
     }

  ]
},

{

  "key":"EM",
  "values":[
     {
        "x":"DHL",
        "y":5
     },
     {
        "x":"Fedex",
        "y":6
     },
     {
        "x":"UPS",
        "y":3
     },
     {
        "x":"TNT",
        "y":5
     }
  ]
}
]

How can i do this using jquery?

freakish
  • 54,167
  • 9
  • 132
  • 169
user2450679
  • 15
  • 1
  • 8
  • 1
    First of all: that's not a job for jQuery but for pure JavaScript. Secondly: [what have you tried](http://mattgemmell.com/2008/12/08/what-have-you-tried/)? – freakish Aug 07 '13 at 09:50
  • The resulting Json is different in the matching values, you need some calculation?? – Daniele Aug 07 '13 at 09:51
  • [Parse the JSON](http://stackoverflow.com/q/4935632/218196), do [object/array modifications in JavaScript](http://stackoverflow.com/q/11922383/218196) and [convert the result back to JSON](http://stackoverflow.com/q/2295496/218196). If you have any specific problem with that, you can ask a question. – Felix Kling Aug 07 '13 at 09:57
  • this output format seems rather inefficient, having an object key named "key" makes it a bit redundant, imho – SirDarius Aug 07 '13 at 10:05

3 Answers3

0

Use var obj = jQuery.parseJSON() to parse your input string. You can access the fields via obj.[FIELDNAME] and build your outputstring.

user229263
  • 21
  • 2
0

You can start with something like this:

$.each(a.data.dataArray,function(){
   //use this[] with the index
});

$.each(a.data.dataHeaders,function(){
  //use this for the headers values
});

and construct your output object.

Daniele
  • 1,938
  • 16
  • 24
0

As I had a little freetime during lunch I gave this a shot, this should get you going:

var before = {
    "message": "Successfully Advisor data has been fetched.",
    "success": true,
    "data": {
        "dataHeaders": ["Carrier","B2B", "EM"],    
        "dataArray": {
            "DHL": ["DHL", "45", "5","5"],
            "Fedex": ["Fedex","40", "7","6"],
            "UPS" : ["UPS","30", "10","3"],
            "TNT" : ["TNT","30", "10","3"],
        }
    }
};


parseData = function (jsonData) {
    var result = [];
    jsonData.data.dataHeaders.forEach(function (carrier, indexHeader) {
        var el = {}
        el.key = carrier;

        el.values = []; 

        // shortcut
        dataArray = jsonData.data.dataArray;
        // generate sub elements
        for (arr in dataArray) {

            // generate element
            subEl = {};
            subEl.x = arr;
            subEl.y = dataArray[arr][indexHeader + 1];
            // add to array
            el.values.push(subEl);
        }

        // add el to result
        result.push(el);
    });
  return result;
}

var after = parseData(before);

JSFiddle

tmaximini
  • 8,403
  • 6
  • 47
  • 69