166

I have this JSON data:

{
    "employees": [
        {
            "firstName": "John",
            "lastName": "Doe"
        },
        {
            "firstName": "Anna",
            "lastName": "Smith"
        },
        {
            "firstName": "Peter",
            "lastName": "Jones"
        }
    ]
}

Suppose I don't know how many columns and rows of employees I have, how do I create this object in JavaScript (Without concate strings)? Assume that I get each row in "onGeneratedRow" method, and I need to push each column (firstName, lastName) to the '{}' brackets.

var viewData = { 
    employees : [] 
};

var rowNum = -1; 

function onGeneratedRow(columnsResult)
{
    rowNum = rowNum + 1;
    viewData.employees.push({});    
    columnsResult.forEach(function(column) {                  
    var columnName = column.metadata.colName;
    viewData.employees[rowNum][columnName] = column.value;  });
}
Waqar Alamgir
  • 9,828
  • 4
  • 30
  • 36
ohadinho
  • 6,894
  • 16
  • 71
  • 124
  • 2
    What is `columnsResult`? What is `metadata`? – georg May 12 '13 at 12:14
  • 1
    This questions does not make sense to me at present, can you explain further, where is your data coming from and in what format. Are you just adding to existing data or creating all of it from scratch. Maybe you can create a jsfiddle to demonstrate what the problem you are having is.Is your question really just, how to access data within an array/ or javascript object? And lets be clear about the data, JSON or Javascript object: http://stackoverflow.com/questions/8294088/javascript-object-vs-json – Xotic750 May 12 '13 at 12:20
  • 2
    assume "columnName" is "firstName" and "column.value" is the value (for example: "John"). I just need to know how to push them dynamically into the brackets ('{}') – ohadinho May 12 '13 at 12:24
  • 1
    What happens when you run the code you've shown? – nnnnnn May 12 '13 at 12:34
  • @ohadinho it is unclear what is your input data strucuture (json) `columnsResult` – Kamil Kiełczewski Apr 25 '19 at 10:24

4 Answers4

195

This is what you need!

function onGeneratedRow(columnsResult)
{
    var jsonData = {};
    columnsResult.forEach(function(column) 
    {
        var columnName = column.metadata.colName;
        jsonData[columnName] = column.value;
    });
    viewData.employees.push(jsonData);
 }
Waqar Alamgir
  • 9,828
  • 4
  • 30
  • 36
134

Perhaps this information will help you.

var sitePersonel = {};
var employees = []
sitePersonel.employees = employees;
console.log(sitePersonel);

var firstName = "John";
var lastName = "Smith";
var employee = {
  "firstName": firstName,
  "lastName": lastName
}
sitePersonel.employees.push(employee);
console.log(sitePersonel);

var manager = "Jane Doe";
sitePersonel.employees[0].manager = manager;
console.log(sitePersonel);

console.log(JSON.stringify(sitePersonel));
Xotic750
  • 22,914
  • 8
  • 57
  • 79
20

This topic, especially the answer of Xotic750 was very helpful to me. I wanted to generate a json variable to pass it to a php script using ajax. My values were stored into two arrays, and i wanted them in json format. This is a generic example:

valArray1 = [121, 324, 42, 31];
valArray2 = [232, 131, 443];
myJson = {objArray1: {}, objArray2: {}};
for (var k = 1; k < valArray1.length; k++) {
    var objName = 'obj' + k;
    var objValue = valArray1[k];
    myJson.objArray1[objName] = objValue;
}
for (var k = 1; k < valArray2.length; k++) {
    var objName = 'obj' + k;
    var objValue = valArray2[k];
    myJson.objArray2[objName] = objValue;
}
console.log(JSON.stringify(myJson));

The result in the console Log should be something like this:

{
   "objArray1": {
        "obj1": 121,
        "obj2": 324,
        "obj3": 42,
        "obj4": 31
   },
   "objArray2": {
        "obj1": 232,
        "obj2": 131,
        "obj3": 443
  }
}
Adrian Wiik
  • 469
  • 1
  • 5
  • 16
1

JavaScript

var myObj = {
   id: "c001",
   name: "Hello Test"
}

Result(JSON)

{
   "id": "c001",
   "name": "Hello Test"
}
Tirolel
  • 928
  • 3
  • 17
  • 43
Ishan Lakshitha
  • 365
  • 3
  • 6