Solved

json conversion with javascript/jquery

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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Avoid defining the variables in the global scope; trying to define them in a local function scope. Because:   • Look-up is performed every time a variable is accessed.   • Variables are resolved backwards from most specific to least specific scope…
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…

746 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now