1

I have a 2-D list from a server URL which has a format of

[["John",5,7,"something1"],
["David",4,2,"something2"],etc]

Is there a fast way to parse this list to a proper JSON format with assigned key pairs as follows?

[{name:"John", 
numbercolumn1:5,
numbercolumn2:7,
description:"something1"},
{},{}]
2
  • this "John,5,7,"something1"...17 items in total is invalid string notation Commented Nov 24, 2017 at 21:38
  • Make a mapping object, like {"0": "name", "1": "numbercolumn1", "2": "numbercolumn2", "3":"description" ... } and then iterate over your outer array, and your inner array, mapping the numbered indexes to the new field names. for ( var i = 0, l = innerArray.length; i < l; i++) { newObj[mappingArray['' + i]] = innerArray[i]; } Commented Nov 24, 2017 at 21:41

4 Answers 4

3

If these are all your properties you can use Array#map() like this:

var data = arr.map(function(a) {
  return {
    name: a[0],
    columnNumber1: a[1],
    columnNumber2: a[2],
    description: a[3]
  };
});

Demo:

var arr = [
  ["John", 5, 7, "something1"],
  ["David", 4, 2, "something2"]
];

var data = arr.map(function(a) {
  return {
    name: a[0],
    columnNumber1: a[1],
    columnNumber2: a[2],
    description: a[3]
  };
});

console.log(data);

Sign up to request clarification or add additional context in comments.

Comments

1
  • You can create an array with the name of the property of the object first.
  • After that you can use Array.prototype.map to iterate on your list a generate the new list of object.
  • Iterate on the property name list to generate the object dynamicaly.

const bigList = [
  ['John', 1, 2],
  ['Doe', 5, 6]
];

const propertyNames = ['name', 'col1', 'col2'];

const newList = bigList.map( item => {
  const newItem = {};
  propertyNames.forEach( (propertyName, index) => {
    newItem[propertyName] = item[index];
  });
  
  return newItem;
});

console.log(newList)

Comments

1

let data = [["John",5,7,"something1"],
            ["David",4,2,"something2"]];
            
let newData = data.map(d => (
    {name: d[0], numbercolumn1: d[1], numbercolumn2: d[2], description: d[3]}
)) 

console.log(newData)

Or without es6 arrow functions:

var data = [["John",5,7,"something1"],
            ["David",4,2,"something2"]];

var newData = data.map(function(d) {
  return {
    name: d[0], numbercolumn1: d[1], numbercolumn2: d[2], description: d[3]
  }
})

console.log(newData)

Hope this helps

Comments

1

With ES6 you could use a destructuring assignment and short hand properties for the object.

var data = [["John", 5, 7, "something1"], ["David", 4, 2, "something2"]],
    result = data.map(([name, numbercolumn1, numbercolumn2, description]) => ({ name, numbercolumn1, numbercolumn2, description }));

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

Comments

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.