Solved

Advanced json just need the record object block

Posted on 2014-10-16
2
133 Views
Last Modified: 2014-12-02
Lets say I have the following JSON OBJECT

{
    "teamname": "Anaheim Ducks",
    "nickname": "Ducks",
    "SportTypes": {
      "-JZLOh7KtM78I5wAL6Ua": true
    },
    "locations": {
      "-JZLCFtpcuvsYxb5N1Cc": true
    },
    "countries": {
      "-JZG9IwEwKGqsExky7UT": true
    },
    "leagues": {
      "-JZLd1hXyyr9ehLwmmbc": true
    },
    "Divisions": {
      "-JZLh2UYKi5IvphLtX4I": true,
      "-JZLh2UaZbpcO7XixD1x": true
    }
  }

Open in new window



I want to loop thru it and pull the record chunks so if the loop pointer position is at 0 all the following JSON chunk will be stored in a variable.

{
    "teamname": "Anaheim Ducks",
    "nickname": "Ducks",
    "SportTypes": {
      "-JZLOh7KtM78I5wAL6Ua": true
    },
    "locations": {
      "-JZLCFtpcuvsYxb5N1Cc": true
    },
    "countries": {
      "-JZG9IwEwKGqsExky7UT": true
    },
    "leagues": {
      "-JZLd1hXyyr9ehLwmmbc": true
    },
    "Divisions": {
      "-JZLh2UYKi5IvphLtX4I": true,
      "-JZLh2UaZbpcO7XixD1x": true
    }
  }

Open in new window

0
Comment
Question by:stargateatlantis
[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 Comments
 
LVL 43

Accepted Solution

by:
Rob earned 250 total points
ID: 40385920
What didn't this do? http://jsbin.com/sunica/1/edit?js,console

the following would store the chunk in a variable
var chunk = myobject[0];
0
 
LVL 63

Assisted Solution

by:Zvonko
Zvonko earned 250 total points
ID: 40411823
If you parse the the JSON string into variable then is that variable the variable that you asked for. So what is the question?

Here a loop proving that the generated object from your JSON string gets all the properties as above stated:
<script>
var strText = '{\
    "teamname": "Anaheim Ducks",\
    "nickname": "Ducks",\
    "SportTypes": {\
      "-JZLOh7KtM78I5wAL6Ua": true\
    },\
    "locations": {\
      "-JZLCFtpcuvsYxb5N1Cc": true\
    },\
    "countries": {\
      "-JZG9IwEwKGqsExky7UT": true\
    },\
    "leagues": {\
      "-JZLd1hXyyr9ehLwmmbc": true\
    },\
    "Divisions": {\
      "-JZLh2UYKi5IvphLtX4I": true,\
      "-JZLh2UaZbpcO7XixD1x": true\
    }\
  }';
  
var obj = JSON.parse(strText);

var msg = "";
for(var elem in obj){
  if(typeof obj[elem] == "object"){
    msg += elem+" {\n";
	for(var attr in obj[elem]){
	  msg += attr+": "+obj[elem][attr]+"\n";
	}
	msg += "}\n";
  } else {
    msg += elem+": "+obj[elem]+"\n";
  }
}

alert(msg);

</script>

Open in new window

0

Featured Post

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
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…
Suggested Courses
Course of the Month10 days, 13 hours left to enroll

632 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