Search code examples
jqueryjsongoogle-apigoogle-search-api

How to parse "og:description" node in json object from Google Custom Search API


I'm using Google custom search API which provides me with results of a search in json. (Docs)

I need to fetch the content of the node og:description but apparently it's not as simple as I thought. I have tried using this: items.pagemap.metatags[0].og:description which won't work - the semi colons cause an error.

Screenshot from console

The rest of my code looks like this and is working:

    var key = "xxx";
    var cx = "xxx";
    var q = "cars";
    var url = "https://www.googleapis.com/customsearch/v1?q=" + encodeURIComponent(q) + " &prettyPrint=false&cx=" + cx + "&key=" + key + "";
    $.getJSON(url, function(data) {
        var news = [];
        if (data.items) {
            $.each(data.items, function(key, i) {
                news.push("<li><img src='" + i.pagemap.cse_image[0].src + "'><a target='_blank' href='" + i.link + "'>" + i.title + "</a><div class='description'>" + i.snippet + "</div></li>");
                //console.log(data);
            });
        }

        $("<ul/>", {
            html: news.join("")
        }).appendTo("#content_0");
    }).fail(function() {
        console.log("error");
    });

Any help is much appreciated.


Solution

  • After hours of searching, it turns out that the solution is this - square brackets:

    items.pagemap.metatags[0]['og:description']