2

Possible Duplicate:
Selecting a JSON object with a colon in the key

I apologize if this is a duplicate question. I searched, I really did!

What I'm trying to achieve is a simple date re-format into something nicer like "Friday, March 9, 2012". I would LOVE to use one of the many convenient jQuery plugins to parse the readily available "pubDate" value into something more useful. Unfortunately there are forces preventing me from importing any other scripts, including jQuery UI. The page template mandated by my superiors imports jQuery and that's it.

My JSON data contains the following snippet:

"items": [
            {
                "title": "blah blah", 
                "link": "http://url.blah.com", 
                "category": "category blah", 
                "pubDate": "Fri, 09 Mar 2012 16:16:05 -0500", 
                "y:published": {
                    "hour": "21", 
                    "timezone": "UTC", 
                    "second": "5", 
                    "month": "3", 
                    "month_name": "March", 
                    "minute": "16", 
                    "utime": "1331327765", 
                    "day": "9", 
                    "day_ordinal_suffix": "th", 
                    "day_of_week": "5", 
                    "day_name": "Friday", 
                    "year": "2012"
                }, 
                "y:id": {
                    "permalink": "true", 
                    "value": null
                }, 
                "y:title": "blah blah", 
                "description": "more blah blah"
            }

If I'm looping over "items" using $.each, how do I retrieve the values for stuff in "y:published"?

Obviously something like

items.y:published.day_name

doesn't work because of the colon. Alas, I am not the creator of this content (it's actually the JSON feed from a Yahoo Pipe, which would probably explain the "y:"); I'm simply tasked with manipulating it. From what I've read, the "y:blahblah" entries are non-standard JSON (?) and probably not parsed via .getJSON, in which case I'm screwed. (Sub-question: is this assessment correct?)

(And just so I cover all my bases here: changing the Yahoo Pipe output from JSON to RSS/XML eliminates the "y:published" node altogether, so that's not an option.)

Thanks in advance. I have no pride; I would appreciate even the most forehead-slappingly simple solution, as long as it could be done with straight js or jQuery.

UPDATE: Answered in record time! Thanks to everyone who contributed.

The solution:

var niceDate =
singleItem['y:published'].day_name + ', ' +
singleItem['y:published'].month_name + ' ' +
singleItem['y:published'].day + ', ' +
singleItem['y:published'].year;
Community
  • 1
  • 1
Paul D
  • 23
  • 3
  • have u seen this? http://stackoverflow.com/questions/4925760/selecting-a-json-object-with-a-colon-in-the-key – redDevil Mar 10 '12 at 07:08
  • FUDGE! No. I had NOT seen that. Thank you so much. *currently slapping my forehead* – Paul D Mar 10 '12 at 07:15
  • Double-thanks. Actually, I HAVE used that kind of notation before in DOM manipulation. I guess I had somehow convinced myself that JSON traversal had an inherently different set of rules. Or something. I don't know why. It's late... – Paul D Mar 10 '12 at 07:17
  • 1
    @PaulD: Once you've deserialized it, you're no longer dealing with JSON at all. You're dealing with JavaScript objects. – T.J. Crowder Mar 10 '12 at 07:19
  • Thanks TJ . I wish I'd had the clarity of mind to realize that in the first place. As I said, I'm familiar with bracketed notation and have used it before in run-of-the-mill DOM stuff. I just got all turned around thinking JSON traversal was somehow different. But you're right of course; anything from DOM nodes, to XML, to JSON is manipulated in the same way once it's been parsed. @achusonline was first, but your answer below was the most comprehensive and thus I've marked it "accepted." – Paul D Mar 10 '12 at 07:34

2 Answers2

4

object["prop"] is equivalent to object.prop, except that the former is not restricted to valid JavaScript identifiers. (All property names in JavaScript are really strings internally. The latter form is for convenience, but as noted, doesn't always work.)

Happy coding.

1

items is an array, so to get the first item, you use items[0].

Then to access the properties on that item that have invalid identifier names, you can use bracketed notation, so:

console.log(items[0]["y:published"].hour); // 21

In JavaScript, you can access object properties in two ways: With dotted notation and a literal (e.g., foo.bar), or with bracketed notation and a string (foo["bar"]). The two are completely interchangeable, but with the string form the property name doesn't have to conform to the rules for JavaScript identifier literals.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875