Build DOM elements from JSON / JQuery

I've got a JSON string that takes this form...

{"people":[{"name":"person1","description":"This is a brief description of my service.","image":"mandg1.jpg","type":"first","insured":"yes","license":"N\/A","picture":"mandg1.jpg","yearsinbusiness":"8","distance":15},{"name":"person2","description":"This is a brief description of my service.","image":"mandg2.jpg","type":"second","insured":"yes","license":"N\/A","picture":"mandg2.jpg","yearsinbusiness":"8","distance":19}]}

What I'd like to do is, via jQuery, build a group of divs based upon the "people" key...so in the instance above...you'd end up creating 2 elements...and append them to the div id="container".  If there were 50 elements, you'd end up with 50 divs, etc etc etc.

<div id="container">
<div id="person1">
<p>Name: person1</p>
<p>Description: This is a brief description of my service</p>
</div>
<div id="person2">
<p>Name: person2</p>
<p>Description: This is a brief description of my service</p>
</div>
</div>

What would the jQuery syntax for that be?  Thanks in advance!
LVL 2
erzoolanderAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

hieloCommented:
Try:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>
<head>
<title>Untitled</title>
<style type="text/css">
<!--
#container {border:1px solid red;}
.person{border:1px solid green;margin:2px;}
-->
</style>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script type="text/javascript">
var data = {
	"people": [{
		"name": "person1",
		"description": "This is a brief description of my service.",
		"image": "mandg1.jpg",
		"type": "first",
		"insured": "yes",
		"license": "N\/A",
		"picture": "mandg1.jpg",
		"yearsinbusiness": "8",
		"distance": 15
	}, {
		"name": "person2",
		"description": "This is a brief description of my service.",
		"image": "mandg2.jpg",
		"type": "second",
		"insured": "yes",
		"license": "N\/A",
		"picture": "mandg2.jpg",
		"yearsinbusiness": "8",
		"distance": 19
	}]
}

$(function(){
	$('<div id="container"/>').appendTo('body');

	$.each(data.people,function(index,item){
		var div = $('<div id="'+item.name+'" class="person"/>').appendTo('#container');
		$('<p>Name: '+item.name+'</p>').appendTo(div);
		$('<p>'+item.description+'</p>').appendTo(div);
	});

});
</script>
</head>
<body>
	
</body>
</html>

Open in new window

0
nap0leonCommented:
I broke it into steps to make it easy to follow/modify:

var theStuff = {"people":[{"name":"person1","description":"This is a brief description of my service.","image":"mandg1.jpg","type":"first","insured":"yes","license":"N\/A","picture":"mandg1.jpg","yearsinbusiness":"8","distance":15},{"name":"person2","description":"This is a brief description of my service.","image":"mandg2.jpg","type":"second","insured":"yes","license":"N\/A","picture":"mandg2.jpg","yearsinbusiness":"8","distance":19}]};
var thePeople = theStuff['people'];

var htmlPeople = $.map(thePeople,function(i){
  var thisHTML = '<div id="' + i.name + '"><p>Name: ' + i.name + '</p><p>Description: ' + i.description + '</p></div>';  
  return (thisHTML);
}).join('');

var theContainer = '<div id="container">' + htmlPeople + '</div>';

Open in new window


This does not have any error handling in it (if the JSON contains an apostrophe, the whole thing breaks).  

Also, you probably want to use the index of the object to ID your divs rather than the person's name like the example shows:

Removing the intermediate steps and using the index instead of the name as the div id looks like this:
var theStuff = {"people":[{"name":"person1","description":"This is a brief description of my service.","image":"mandg1.jpg","type":"first","insured":"yes","license":"N\/A","picture":"mandg1.jpg","yearsinbusiness":"8","distance":15},{"name":"person2","description":"This is a brief description of my service.","image":"mandg2.jpg","type":"second","insured":"yes","license":"N\/A","picture":"mandg2.jpg","yearsinbusiness":"8","distance":19}]};

var htmlPeople = '<div id="container">' + $.map(theStuff['people'],function(i,idx){
  var thisHTML = '<div id="person' + idx + '"><p>Name: ' + i.name + '</p><p>Description: ' + i.description + '</p></div>';  
  return (thisHTML);
}).join('') + '</div>';

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
erzoolanderAuthor Commented:
Gracias!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.