Tech or Treat! Write an article about your scariest tech disaster to win gadgets!Learn more

x
?
Solved

JSON Parsing

Posted on 2011-03-10
5
Medium Priority
?
750 Views
Last Modified: 2013-11-08
Hello, am having some difficulty parsing the following JSON:
{
    "references": [{
        "type": "user",
        "full_name": "Jim Bo Bob",
        "id": 5,
        "url": "https://yahoo.com",
    }, {
        "type": "user",
        "full_name": "Bob Joe Jim",
        "id": 11,
        "url": "https://google.com",
    }, {
        "type": "topic",
        "name": "Muscular Arms",
        "id": 28
    }, {
        "voted_user_id": 5,
        "type": "approved",
        "approved_user_id": 7,
        "topic_id": 28,
        "comment": "His arms are massive",
        "id": 297
    }, {
        "voted_user_id": 11,
        "type": "approved",
        "approved_user_id": 7,
        "topic_id": 28,
        "comment": "Very moist",
        "id": 89
    }]
}

Open in new window


I need to be able to loop through each object and determine when references.id == references.voter_user_id.  For example, parsing through the JSON above, I should find a match between the first object (references.id = 5) and the fourth object (references.voter_user_id: 5).

Am currently using the following jquery js to loop:

$.each(data.references, function (i, ref) {
	if(ref.id == ref.voter_user_id) {
		// do stuff
	}
});

Open in new window


That seems to check *within* each object whether ref.id == ref.voter_user_id and this is not what I want.
0
Comment
Question by:aznprncritic
[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
  • 3
5 Comments
 
LVL 82

Expert Comment

by:leakim971
ID: 35094460
voted_user_id versus
voter_user_id

no ?
0
 

Author Comment

by:aznprncritic
ID: 35094544
Sorry, must have been a typo.  The problem is my js is checking *within* each object for a match between ref.id and ref.voted_user_id.
0
 
LVL 82

Accepted Solution

by:
leakim971 earned 2000 total points
ID: 35094621
Check this (we can do better if both are not in the same array) :
<!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>json find peer same array</title>
<script language="javascript" src="http://code.jquery.com/jquery-1.5.1.min.js"></script>
<script language="javascript">
	var data = {"references":[{"type":"user","full_name":"Jim Bo Bob","id":5,"url":"https://yahoo.com",},{"type":"user","full_name":"Bob Joe Jim","id":11,"url":"https://google.com",},{"type":"topic","name":"Muscular Arms","id":28},{"voted_user_id":5,"type":"approved","approved_user_id":7,"topic_id":28,"comment":"His arms are massive","id":297},{"voted_user_id":11,"type":"approved","approved_user_id":7,"topic_id":28,"comment":"Very moist","id":89}]};
	$(document).ready(function() {
		var json = data.references;
		$.each(data.references, function (i, ref) {
			for(var j in json) {
				if(ref.id == json[j]["voted_user_id"]) {
					alert( "full name is : " + ref["full_name"] + "\ntype is : "+ ref.type );
				}
			}
		});
	});
</script>
</head>
<body>
</body>
</html>

Open in new window

0
 
LVL 19

Expert Comment

by:Albert Van Halen
ID: 35094675
Your references array contains different objects : users, topic and approved.
You need to filter them all out by their type -> all users, the topic and approved items.
var users = references.filter(function(ref) { return ref.type=="user"; });
var approvedItems = references.filter(function(ref) { return ref.type=="approved"; });
var topics = references.filter(function(ref) { return ref.type=="topic"; });

// now you can loop through all your approved items
var numApproved = approvedItems.length;
for(var x = 0; x < numApproved; x++) {
   var approvedItem = approvedItems[x];
   var user = users.filter(function(user) { return user.id == approvedItem.approved_user_id; });
   if(user.length > 0) {
      // user array containing all the users with id equel to approvedItem.approved_user_id
   }
}

Open in new window

0
 
LVL 82

Expert Comment

by:leakim971
ID: 35097786
Check this useful online tools : http://jsonviewer.stack.hu/
Paste you json object in the window, you can format or parse it easily graphically (click on the Viewer tab)
Thanks for the points!

Clipboard02.jpg
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

PROBLEM: The other day I was working on adding an ajax request to a webpage that already had a dialog box on the page.  The dialog box was using relative positioning to be positioned next to a form field I had on the page.  Everything was working…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to dynamically set the form action using jQuery.
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…
Suggested Courses

647 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