Solved

json conversion with javascript/jquery

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone 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

Suggested Solutions

Title # Comments Views Activity
Print Rhino Java Array in Javascript 1 30
Use "if not" in a condition 2 18
Accordion won't close correctly 5 12
what should i learn? javascript or typescript? 3 33
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
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…

735 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