Skip to main content
Module

x/serpapi/mod.ts>getJson

Scrape and parse search engine results using SerpApi.
Go to Latest
function getJson
import { getJson } from "https://deno.land/x/serpapi@1.1.1/mod.ts";

Get a JSON response based on search parameters.

  • Accepts an optional callback.
  • Get the next page of results by calling the .next() method on the returned response object.

Examples

// single call (async/await) const json = await getJson("google", { api_key: API_KEY, q: "coffee" });

// single call (callback) getJson("google", { api_key: API_KEY, q: "coffee" }, console.log);

// pagination (async/await) const page1 = await getJson("google", { q: "coffee", start: 15 }); const page2 = await page1.next?.();

// pagination (callback) getJson("google", { q: "coffee", start: 15 }, (page1) => { page1.next?.((page2) => { console.log(page2); }); });

// pagination loop (async/await) const organicResults = []; let page = await getJson("google", { api_key: API_KEY, q: "coffee" }); while (page) { organicResults.push(...page.organic_results); if (organicResults.length >= 30) break; page = await page.next?.(); }

// pagination loop (callback) const organicResults = []; getJson("google", { api_key: API_KEY, q: "coffee" }, (page) => { organicResults.push(...page.organic_results); if (organicResults.length < 30 && page.next) { page.next(); } });

Type Parameters

E extends keyof EngineMap

Parameters

engine: E
  • engine name
parameters: EngineMap[E]["parameters"]
  • search query parameters for the engine
optional
callback: (json: BaseResponse<EngineMap[E]["parameters"]>) => void
  • optional callback