Andreas_Lng
Andreas_Lng

Reputation: 1197

Node-RED parse json received from wikimedia api

I'm new to node-red and want to parse content received from wikipedia api. I send requests to the query endpoint:

https://en.wikipedia.org/w/api.php?action=query&titles={{{query}}}&prop=revisions&rvprop=parsetree&format=json&rvsection=0

The response looks similar to this:

{
  ...,
  "query": {
    "normalized": [ ... ],
    "pages": {
      "123456789": {
        "pageid": 123456789,
        "ns": 0,
        "title": "title",
        "revisions": [{
          "parsetree": "...."
        }]
      }
    }
  }
}

I need to parse the content of parsetree, but am unable to get the first json object of pages dynamically.

Of course I can do something like: msg.payload.query.pages.123456789.revisions[0].parsetree

But I have a lot of titles i like to query and to process.

Is there an other way to get the content of parsetree?

Upvotes: 0

Views: 99

Answers (1)

hardillb
hardillb

Reputation: 59751

You can always get hold of the list of keys in an object using the Object.keys(obj) method (doc)

So something like this should work

var pages = Object.keys(msg.payload.query.pages);
for (var i=0; i<pages.length; i++) {
  var parsetree = msg.payload.query.pages[pages[i]].revisions[0].parsetree;
  ...
}

Upvotes: 1

Related Questions