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
Solved

json conversion with javascript/jquery

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
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…

828 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