0

I have a JavaScript object in the format

{
    "header": {
        "dataFields": ["number", "name", "quantity", "id", "from"]
    },
    "data": [
        "20|sam|12|2|2012-06-29T00:00Z|",
        "18|peter|231|12|",
    ]
}

I am trying to get it in to this format:

[{"number" : "20", "name":"sam", "quantity" : "12", "id":"2"},
 {"number" : "18", "name":"peter", "quantity": "231", "id" 12"}]

I dont want the field "from in the output array.. what could be the best way to acheive it??

var l={};

for ( var key in responseData.positions[i]){
    l.name=key;
        for(var k=0; k<responseData.positions.length;k++){
            for ( var key in responseData.positions[k]) {
                l.value= responseData.positions[k][key] ;
            }
        }
mainArray.push(l);
4
  • So is this about JSON or JavaScript? Even if you get the original data as JSON, once you parsed it into native data types, it has nothing to do with JSON anymore. I actually assume you are having an object and want to convert it into an array of objects. Commented Oct 18, 2012 at 6:24
  • @Barmar IVe added what Ive tried in to the question Commented Oct 18, 2012 at 6:29
  • Your code doesn't bear any resemblance to the data structure. I assume responseData contains the object, but what is positions? Why aren't you using responseData.header or responseData.data? Where are you setting i? And your braces don't match, you have 3 opens and only 2 closes. Commented Oct 18, 2012 at 6:36
  • responseData.positions is the input array Commented Oct 18, 2012 at 6:39

4 Answers 4

3

You can use the array map method to iterate through the data array and convert it.

Code and live demo: http://jsfiddle.net/N9QyQ/

var object={
"header": {
    "dataFields": ["number", "name", "quantity", "id", "rom"]
},
"data": [
    "20|sam|12|2|2012-06-29T00:00Z",
    "18|peter|231|12|"
 ]
} ;
var result=object.data.map(function(string){
    var split=string.split("|"), item={};
    for (var i=0;i<4;i++) {
        item[object.header.dataFields[i]]=split[i];
    }
    return item;
});
Sign up to request clarification or add additional context in comments.

Comments

1
mainArray = [];
for (i = 0; i < responseData.positions.data.length; i++) {
    var dataArray = responseData.positions.data[i].split("|");
    var newObject = {};
    for (j = 0; j < responseData.positions.header.length; j++) {
        if (responseData.positions.header[j] != "from") {
            newObject[responseData.positions.header[j]] = dataArray[j];
        }
    }
    mainArray.push(newObject);
}

Comments

0
var obj = {
    "header": {
        "dataFields": ["number", "name", "quantity", "id", "from"]
    },
    "data": [
        "20|sam|12|2|2012-06-29T00:00Z|",
        "18|peter|231|12|"
    ]
};
var result = [];
for(var i = 0, headers = obj.header.dataFields, len = obj.data.length, hLen = headers.length; i < len; i++) {
    var split = obj.data[i].split("|");
    var entry = {};
    // assuming you want first four elements
    /*
    for(var j = 0; j < 4; j++) {
        entry[headers[j]] = split[j];
    }
    */
    // assuming you want to exclude from field
    for(var j = 0; j < hLen; j++) {
        if(headers[j] == "from")
            continue;
        entry[headers[j]] = split[j];
    }       
    result.push(entry);
}

Comments

0

If you want to use jQuery you could do the following

jsonData = {
    "header": {
        "dataFields": ["number", "name", "quantity", "id", "from"]
    },
    "data": [
        "20|sam|12|2|2012-06-29T00:00Z|",
        "18|peter|231|12|",
    ]
};


tmpData = {}
newData = []

$.each(jsonData['data'], function () {
    data = this;
    $.each(jsonData['header']['dataFields'], function (index, header) {
        if (header !== "from") {
            tmpData[header] = data.split('|')[index]
        }
    });
    newData.push(tmpData);
    tmpData = {};
});

newData will contain

[
    {id: "2", name: "sam",number: "20",quantity: "12"},
    { id: "12", name: "peter", number: "18", quantity: "231" }
]

You can check the fiddle here

3 Comments

The map method I mentioned in my answer also exists in jQuery ($.map)
sorry, I wasnt trying to undermine your answer. I had written this answer a while back but forgot to post it
no worries :-) I was just commenting your code and pointing to a shorter method.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.