Solved

json conversion with javascript/jquery

Posted on 2011-03-06
2
212 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
  • 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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

In this article, we'll look how to sort an Array in JavaScript, including the more advanced techniques of sorting a collection of records either ascending or descending on two or more fields. Basic Sorting of Arrays First, let's look at the …
This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
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…

770 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