Asif
Asif

Reputation: 393

How to add array of JSON objects to Javascript array

I have an array of JSON objects that I want to create programmatically. The output array required is as follows

Actual Array of JSON object required

The current code is as follows

var arrData = [];
  arrData[0] = '{\"GeneralInformation\": [{\"BusinessSummary\": \"Apple Inc. designs, manufactures and markets mobile communication and media devices, personal computers and portable digital music players and sells a variety of related software, services, peripherals, networking solutions and third-party digital content and applications. The Company\'s; products and services include iPhone, iPad, Mac, iPod, Apple TV, a portfolio of consumer and professional software applications, the iOS and OS X operating systems, iCloud and a variety of accessory, service and support offerings. The Company offers a range of mobile communication and media devices, personal computing products and portable digital music players, as well as a variety of related software, services, peripherals, networking solutions and third-party hardware and software products. The Company\'s primary products include iPhone, iPad, Mac, iPod, iTunes, Mac App Store, iCloud, Operating System Software, Application Software and Other Application Software.\"},{\"Websites\": [{\"Homepage\": \"http:www.apple.com\"}]}]}';

  console.log(arrData[0]);

From the code above I am getting the output as below

output obtained

As we can see the output is all text. How can I output the array of JSON object as shown in first image from the code listed above.

Upvotes: 2

Views: 100

Answers (3)

jay
jay

Reputation: 176

JSON.parse() converts a string in JSON format to a JavaScript object. Since arrData[0] is a string, in order to access its parameters, you'd need to use JSON.parse()

console.log(JSON.parse(arrData[0]));

Upvotes: 1

Yeldar Kurmangaliyev
Yeldar Kurmangaliyev

Reputation: 34244

As you told, you have an array of JSON strings. You can simply convert it to an array of objects by applying JSON.parse function to every item.

It can be easily done using Array.prototype.map function:

// generates a new array by applying JSON.parse to every item
arrData = arrData.map(JSON.parse); 

// just for better understanding. "map" is almost the same as:
for (var i = 0; i < arrData.length; i++)
{
    arrData[i] = JSON.parse(arrData[i]);
}

After that, arrData will become an array of JavaScript objects.

Working demo:

var arrData = [];
arrData[0] = '{\"GeneralInformation\": [{\"BusinessSummary\": \"Apple Inc. designs, manufactures and markets mobile communication and media devices, personal computers and portable digital music players and sells a variety of related software, services, peripherals, networking solutions and third-party digital content and applications. The Company\'s; products and services include iPhone, iPad, Mac, iPod, Apple TV, a portfolio of consumer and professional software applications, the iOS and OS X operating systems, iCloud and a variety of accessory, service and support offerings. The Company offers a range of mobile communication and media devices, personal computing products and portable digital music players, as well as a variety of related software, services, peripherals, networking solutions and third-party hardware and software products. The Company\'s primary products include iPhone, iPad, Mac, iPod, iTunes, Mac App Store, iCloud, Operating System Software, Application Software and Other Application Software.\"},{\"Websites\": [{\"Homepage\": \"http:www.apple.com\"}]}]}';
arrData[1] = '{\"GeneralInformation\": [{\"BusinessSummary\": \"Apple Inc. designs, manufactures and markets mobile communication and media devices, personal computers and portable digital music players and sells a variety of related software, services, peripherals, networking solutions and third-party digital content and applications. The Company\'s; products and services include iPhone, iPad, Mac, iPod, Apple TV, a portfolio of consumer and professional software applications, the iOS and OS X operating systems, iCloud and a variety of accessory, service and support offerings. The Company offers a range of mobile communication and media devices, personal computing products and portable digital music players, as well as a variety of related software, services, peripherals, networking solutions and third-party hardware and software products. The Company\'s primary products include iPhone, iPad, Mac, iPod, iTunes, Mac App Store, iCloud, Operating System Software, Application Software and Other Application Software.\"},{\"Websites\": [{\"Homepage\": \"http:www.apple.com\"}]}]}';

arrData = arrData.map(JSON.parse);
console.log(arrData);
<div style="color: white;">Wake up Neo...</div>Look at the console

Upvotes: 2

T J
T J

Reputation: 43166

Use JSON.parse method to parse the JSON string into JavaScript object.

console.log(JSON.parse(arrData[0]));

Upvotes: 1

Related Questions