I have a json feed that I would like to extract information out from but for the last few days I've been struggling. I can target the item using the key but because the number of items in the object can vary which obviously then breaks my method.

In any case, the way that you are currently retrieving the data is about as good as it gets, you can of course shorten the path a little if you only need to get access to the reps in a particular section of the code.

// currently you have a rather long path to get to a rep
var rep = data.SiteRep.DV.Location.Period[0].Rep[0];
// you could shorten it like this
var periods = data.SiteRep.DV.Location.Period;
//you can now access the periods/reps like so:
rep = periods[0].Rep[0];

Sorry. It is definately json. However, I have an extension for Chrome that displays the feed like that and I had just cut and paste it without thinking.

Ah yesh. I did see were using JSON notation to address the object's properties.

jamus said:

Is there some way the the value of a Rep object ($ in the JSON representation) can be used to target the Pp from that report?

When you drill down you can just use the dot-notation to access that property, or if for some reason you need to get a property based on another property then you can also use the bracket-notation to do it that way.

e.g.

precipitationPercent = periods[0].Rep[0].Pp

Or

// get the property name from one of the previous objects
// let's say that units[9].name contains the string "Pp"
// based on {"name":"Pp","units":"%","$":"Precipitation Probability"}
precipitationPercent = periods[0].Rep[0][ units[9].name ];

Hope this helps, not 100% sure if I understood your question correctly, feel free to clarify if I haven't

What happens is that when you put "place" as the parameter for the callback function of the .each() method, it uses that as the key.

The jQuery.each() callback can be used in 2 different ways, the first is if you pass in the key/value parameters, the second is by using the "this" keyword the current element that is being iterated over.

In this case, your second example is almost done! All you need to do is use "this" instead of "place".

I purposely used data.Locations.Location[0] here to demo, as this second method is better for looping over objects rather than arrays. You can use that method as well to loop over arrays of course, the key will then be the array index and val will be the value of the item at that index in the array. However, when I know that I'm looping over an array I tend to use the first method as all I'm usually interested in the value.

Would you ever have an array stored in a object? How would I tell them apart in the json feed? Sorry if that's an obvious question!

I guess normally, since you'd be processing the data somehow, you would know which nodes are objects and which are arrays. Though, strictly speaking if you're using the $.each() method you don't need to know whether you're iterating over an array or an object.

Of course you can inspect the JSON object in your debugger (Dev tools, firebug, whatever) to see which nodes are what.

The actual question of whether you could/should/would store an array in an object, it all comes down to "what" the data is. Let me illustrate this in code: