janl / mustache.js

Minimal templating with {{mustaches}} in JavaScript

Home Page:https://mustache.github.io

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Nested json fetch error

diwakaran1dan opened this issue · comments

i need to fetch Url of articles from blogger Json feed (like https://example.blogspot.com/feeds/posts/default?alt=json) when i tried the following Tag it will bring 3 to 4 the urls (i.e., Blogger ID link, comment link, and Article link)

{{#link}}{{href}}{{/link}}

Output:

https://www.blogger.com/feeds/ID-XXXX/posts/default/ID-XXXX
https://www.blogger.com/feeds/ID-XXXX/posts/default/ID-XXXX
https://example.blogspot.com/2020/08/........html

i think it came because there is a nested object in Blogger Json feed...
can you please tell me how to fetch only the url of articles in blogger json feed

const mustache = require("mustache");
const feedResponse = require("./feedResponse.json"); // from a .json file or a HTTPS request

const entries = feedResponse.feed.entry;
const entriesWithUrl = entries.map((entry) => {
  const selfLink = entry.link.find((link) => link.rel === "self").href;

  return {
    title: entry.title.$t,
    url: selfLink,
  };
});

const template = `
{{#.}}
  {{title}}: {{{url}}}
{{/.}}
`;

const result = mustache.render(template, entriesWithUrl);

Could be one way? That gave me the each entry with its title & URL at least.

The trick with mustache.js is do to as much work & preparation of the data before asking mustache to .render(). This is mainly because of the mustache specification being deliberately very limited in terms of how much logic and complexity you can do inside the actual template itself -- that has to be sorted out before rendering.

Closing this for now as it's not really a mustache.js issue in itself, but rather how to interpret JSON and make it easier to traverse in the template.