Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Java by (13.1k points)

Can anyone help me how I can able to create a JSON object in JavaScript without any concate strings? Here is my JSON data and my JavaScript:

{

    "employees": [

        {

            "firstName": "John",

            "lastName": "Doe"

        },

        {

            "firstName": "Anna",

            "lastName": "Smith"

        },

        {

            "firstName": "Peter",

            "lastName": "Jones"

        }

    ]

}

JavaScript:

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;  });

}

Any help would be appreciated.

1 Answer

0 votes
by (26.7k points)

You can change your JavaScript code like the below code and it will work:

function onGeneratedRow(columnsResult)

{

    var jsonData = {};

    columnsResult.forEach(function(column) 

    {

        var columnName = column.metadata.colName;

        jsonData[columnName] = column.value;

    });

    viewData.employees.push(jsonData);

 }

I hope this will help.

Want to know more about Java? Prefer this tutorial on Learn Java.

Want to become a Java Expert? Join Java Certification now!!

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...