persistent json

I want to use the first position in this json array
$.getJSON("somefile", function(json) {
      firstfunction(json[0]);
});

the issue is that I need to call the same json again further down the page
$.getJSON("somefile", function(json) {
      $.each(json, function(key, data) { // this time I loop and call a different function
            secondfunction(key,data);                   
      });
});

I don't have the syntax to call this file once and use the the first position for the first function then loop the entire object for the second function
please advise ...
LVL 25
dgrafxAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Julian HansenCommented:
The main issue is that the getJSON call is asynchronous - so there is no concept of "further down" - the code "further down" could theoretically execute before the first getJSON has completed.

What you should be considering is a single processing of the returned JSON structure - in other words
$.getJSON("somefile", function(json) {
   doRestOfProcessing(json);
});

function doRestOfProcessing(json)
{
   firstfunction(json[0]);
   // CODE HERE
   // ...
   // LOOP HERE
   $.each(json, function(key, data) { // this time I loop and call a different function
       secondfunction(key,data);                   
   });

  // REST OF CODE HERE
  // ...
}

Open in new window

What the above code does is first performs the asynchronous call to get the JSON return. No other processing is done until this call completes. Once it has finished - the doRestOfProcessing() function is called which now synchronously performs the other functions needed using the JSON structure passed in as an argument.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
dgrafxAuthor Commented:
I will work with that and let you know !
Thanks Julian
dgrafxAuthor Commented:
Thanks - had a rough day so just got to it now.
I do appreciate ...
Julian HansenCommented:
You are welcome.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
jQuery

From novice to tech pro — start learning today.