Solved

json conversion with javascript/jquery

Posted on 2011-03-06
2
216 Views
Last Modified: 2012-05-11
K so i have a json file the format is below on the code section named var originalJSON.

I need to get the format into the var finalJSON format using 2 variables that will be referencing the category field of the originalJSON.  So in other words in am looking through the categories section of the originalJSON using my variables and needing to construct the finalJSON off of that.
var first = "corporate";
var second = "products";

var originalJSON = 
[
    {
        "title": "first",
        "category": "corporate",
        "id": "20" 
    },
    {
        "title": "second",
        "category": "products",
        "id": "21" 
    },
    {
        "title": "third",
        "category": "corporate",
        "id": "22" 
    },
    {
        "title": "fourth",
        "category": "animals",
        "id": "23" 
    } 
]; 


var finalJSON = {
    "categories": [
        {
            "categoryname": "corporate",
            "category": [
                {
                    "title": "first",
                    "category": "corporate",
                    "id": "20" 
                },
                {
                    "title": "third",
                    "category": "corporate",
                    "id": "22" 
                } 
            ] 
        },
        {
            "categoryname": "products",
            "category": [
                {
                    "title": "second",
                    "category": "products",
                    "id": "21" 
                } 
            ] 
        } 
    ] 
}

Open in new window

0
Comment
Question by:Brant Snow
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
2 Comments
 
LVL 82

Expert Comment

by:leakim971
ID: 35050993
Check this one (it don't use the variables) :


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script language="javascript">
	var originalJSON = [{"title":"first","category":"corporate","id":"20"},{"title":"second","category":"products","id":"21"},{"title":"third","category":"corporate","id":"22"},{"title":"fourth","category":"animals","id":"23"}];
	var finalJSON = null;

	window.onload = function() {
		finalJSON = { "categories":[] };
		for(var i=0;i<originalJSON.length;i++) {
		   var found = false;
		   for(var j=0;j<finalJSON.categories.length;j++) {
			  if( originalJSON[i]["category"] == finalJSON["categories"][j]["categoryname"] ) {
				   found = true;
				   break;
			  }
		   }
		   if(!found) {
			   finalJSON["categories"][j] = {"categoryname":originalJSON[i]["category"], "category":[]};
		   }
		   finalJSON["categories"][j]["category"].push( originalJSON[i] );
		}
	}

</script>
</head>
<body>
</body>
</html>

Open in new window

0
 
LVL 82

Accepted Solution

by:
leakim971 earned 500 total points
ID: 35051054
This one worry about the two variables :


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script language="javascript">

	var originalJSON = [{"title":"first","category":"corporate","id":"20"},{"title":"second","category":"products","id":"21"},{"title":"third","category":"corporate","id":"22"},{"title":"fourth","category":"animals","id":"23"}];
	var finalJSON = null;

	var first = "corporate";
	var second = "products";

	window.onload = function() {
		finalJSON = { "categories":[] };
		for(var i=0;i<originalJSON.length;i++) {
			originalJSONCategory = originalJSON[i]["category"];
			if( originalJSONCategory == first || originalJSONCategory == second ) {
				var found = false;
				for(var j=0;j<finalJSON.categories.length;j++) {
					if( originalJSONCategory == finalJSON["categories"][j]["categoryname"] ) {
						found = true;
						break;
					}
				}
				if(!found) {
					finalJSON["categories"][j] = {"categoryname":originalJSON[i]["category"], "category":[]};
				}
				finalJSON["categories"][j]["category"].push( originalJSON[i] );
			}
		}
	}

</script>
</head>
<body>
</body>
</html>

Open in new window

0

Featured Post

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In my daily work (mainly using ASP.net), I need to write a lot of JavaScript code. One of the most repetitive tasks I do are the jQuery Ajax calls. You know: (CODE) I don't know if for you it's the same, but for me is soooo tedious to write the …
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

688 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question