6

I'm trying to pull the text out of a Wikipedia article using their API (API is a generous term for what they are offering, but we'll use it I guess), and I am running into issues regarding parsing of the subsequent JSON object I am getting back. Namely, the object contains the text I am looking for under a key whose label is '*' such that, after running the command:

$.getJSON("http://en.wikipedia.org/w/api.php?action=parse&format=json&callback=?", {page:"Red Sea clownfish", prop:"text"}, function(data) {

I then attempt to parse this information into a string using the command:

var dat = data.parse.text.*;

Which I am then outputting to a console using:

console.log(dat);

Unfortunately, neither Google Chrome nor Firefox seem to be able to parse the '*' key. When I dump the full 'data' object into the console, I can see that the (nested) keys for the data structure are 'parse', 'text', and '*'. I can even dump the text I need up to the '*' key. I.e.

var dat = data.parse.text;

works. It's just that '*' character does not want to be recognized.

Any ideas on how to fix this? Ideally I'd like to get access to the value that the '*' key is referencing. I just have no idea how to program it in javascript.

Damjan Pavlica
  • 31,277
  • 10
  • 71
  • 76
Mephistopheles
  • 288
  • 2
  • 9
  • Just out of curiosity, what's with the `callback=?`? (It seems to work the same as an empty callback, presumably because MediaWiki is stripping out the invalid character.) – Ilmari Karonen Nov 14 '11 at 17:01
  • 1
    Yes! Wikipedia/MediaWiki is undoubtedly having the most horrendous API in the world. – Jaseem Aug 08 '12 at 01:50

1 Answers1

8

The * character isn't allowed as part of an identifier (variable name) in JavaScript, so that syntax doesn't work. Instead, you can use the array/subscript notation to access properties using any string, regardless of whether it's a valid identifier:

var dat = data.parse.text['*'];
Jeremy
  • 1
  • 85
  • 340
  • 366