Solved

Advanced json just need the record object block

Posted on 2014-10-16
2
130 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How do I Enable submit button only if listbox has items 4 65
How can I do this in Angular, instead of jQuery? 2 23
Conformation code 4 37
Error just appeared 3 23
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.
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
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…

710 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