2

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.

1
  • try console.log(arrData); Commented Oct 28, 2015 at 11:06

3 Answers 3

2

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

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

Comments

1

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

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

Comments

1

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

2 Comments

Please add some comments about your solution on why and how it solves the problem
@Odedra, thanks for the advice on making my answer more useful.

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.