简体   繁体   中英

Get values from request.body in Node

I am working on a node project which use google search api. I am getting the reponse in the following way:

{
  kind: 'customsearch#search',
  url: {
    type: 'application/json',
    template: 'https://www.googleapis.com/customsearch/v1?q={searchTerms}&num={count?}&start={startIndex?}&lr={language?}&safe={safe?}&cx={cx?}&sort={sort?}&filter={filter?}&gl={gl?}&cr={cr?}&googlehost={googleHost?}&c2coff={disableCnTwTranslation?}&hq={hq?}&hl={hl?}&siteSearch={siteSearch?}&siteSearchFilter={siteSearchFilter?}&exactTerms={exactTerms?}&excludeTerms={excludeTerms?}&linkSite={linkSite?}&orTerms={orTerms?}&relatedSite={relatedSite?}&dateRestrict={dateRestrict?}&lowRange={lowRange?}&highRange={highRange?}&searchType={searchType}&fileType={fileType?}&rights={rights?}&imgSize={imgSize?}&imgType={imgType?}&imgColorType={imgColorType?}&imgDominantColor={imgDominantColor?}&alt=json'
  },
  queries: { request: [ [Object] ] },
  context: { title: 'CS Curriculum', facets: [ [Array], [Array], [Array] ] },
  searchInformation: {
    searchTime: 0.577799,
    formattedSearchTime: '0.58',
    totalResults: '10',
    formattedTotalResults: '10'
  },
  items: [
    {
      kind: 'customsearch#result',
      title: '2021 News | Department of Computer Science',
      htmlTitle: '2021 <b>News</b> | Department of Computer Science',
      link: 'https://www.cs.utexas.edu/news/articles/index2002',
      displayLink: 'www.cs.utexas.edu',
      snippet: 'Campus health and safety are our top priorities. Get the latest from UT on COVID-\n' +
        '19. Get help with Instructional Continuity and working from home. About · News ...',
      htmlSnippet: 'Campus health and safety are our top priorities. Get the latest from UT on <b>COVID</b>-<br>\n' +
        '19. Get help with Instructional Continuity and working from home. About &middot; <b>News</b>&nbsp;...',
      cacheId: '7AHQ45k8IUUJ',
      formattedUrl: 'https://www.cs.utexas.edu/news/articles/index2002',
      htmlFormattedUrl: 'https://www.cs.utexas.edu/<b>news</b>/<b>articles</b>/index2002',
      pagemap: [Object]
    },
    {
      kind: 'customsearch#result',
      title: 'New York Daily News | News Sources | Department of Computer ...',
      htmlTitle: 'New York Daily <b>News</b> | <b>News</b> Sources | Department of Computer ...',
      link: 'https://www.cs.columbia.edu/news-source/new-york-daily-news/',
      displayLink: 'www.cs.columbia.edu',
      snippet: 'Learn more about the COVID-19 Response ... In the News ... New Twitter Feature \n' +
        'Challenges Users to Read Articles Before Sharing (Peter Allen,Daniel Bauer ...',
      htmlSnippet: 'Learn more about the <b>COVID</b>-19 Response ... In the <b>News</b> ... New Twitter Feature <br>\n' +
        'Challenges Users to Read <b>Articles</b> Before Sharing (Peter Allen,Daniel Bauer&nbsp;...',
      cacheId: 'H0RrwEK65C8J',
      formattedUrl: 'https://www.cs.columbia.edu/news-source/new-york-daily-news/',
      htmlFormattedUrl: 'https://www.cs.columbia.edu/<b>news</b>-source/new-york-daily-<b>news</b>/',
      pagemap: [Object]
    },
    {
      kind: 'customsearch#result',
      title: 'Smithsonian.com | News Sources | Department of Computer ...',
      htmlTitle: 'Smithsonian.com | <b>News</b> Sources | Department of Computer ...',
      link: 'https://www.cs.columbia.edu/news-source/smithsonian-com/',
      displayLink: 'www.cs.columbia.edu',
      snippet: 'COVID-19 Response. The Columbia Engineering community has come together \n' +
        'to combat the coronavirus pandemic on multiple fronts. In close collabo-ration ...',
      htmlSnippet: '<b>COVID</b>-19 Response. The Columbia Engineering community has come together <br>\n' +
        'to combat the <b>coronavirus</b> pandemic on multiple fronts. In close collabo-ration&nbsp;...',
      cacheId: 'eLnifJe1zycJ',
      formattedUrl: 'https://www.cs.columbia.edu/news-source/smithsonian-com/',
      htmlFormattedUrl: 'https://www.cs.columbia.edu/<b>news</b>-source/smithsonian-com/',
      pagemap: [Object]
    },
    {
      kind: 'customsearch#result',
      title: '2020 News | Page 2 | Department of Computer Science',
      htmlTitle: '2020 <b>News</b> | Page 2 | Department of Computer Science',
      link: 'https://www.cs.utexas.edu/news/articles/index2002?page=1',
      displayLink: 'www.cs.utexas.edu',
      snippet: 'Campus health and safety are our top priorities. Get the latest from UT on COVID-\n' +
        '19. Get help with Instructional Continuity and working from home.',
      htmlSnippet: 'Campus health and safety are our top priorities. Get the latest from UT on <b>COVID</b>-<br>\n' +
        '19. Get help with Instructional Continuity and working from home.',
      cacheId: 'S47VULaZRu8J',
      formattedUrl: 'https://www.cs.utexas.edu/news/articles/index2002?page=1',
      htmlFormattedUrl: 'https://www.cs.utexas.edu/<b>news</b>/<b>articles</b>/index2002?page=1',
      pagemap: [Object]
    },
    {
      kind: 'customsearch#result',
      title: 'Your Houston News | News Sources | Department of Computer ...',
      htmlTitle: 'Your Houston <b>News</b> | <b>News</b> Sources | Department of Computer ...',
      link: 'https://www.cs.columbia.edu/news-source/your-houston-news/',
      displayLink: 'www.cs.columbia.edu',
      snippet: 'Learn more about the COVID-19 Response ... In the News ... New Twitter Feature \n' +
        'Challenges Users to Read Articles Before Sharing (Peter Allen,Daniel Bauer ...',
      htmlSnippet: 'Learn more about the <b>COVID</b>-19 Response ... In the <b>News</b> ... New Twitter Feature <br>\n' +
        'Challenges Users to Read <b>Articles</b> Before Sharing (Peter Allen,Daniel Bauer&nbsp;...',
      cacheId: 'JRaK3HT4SeYJ',
      formattedUrl: 'https://www.cs.columbia.edu/news-source/your-houston-news/',
      htmlFormattedUrl: 'https://www.cs.columbia.edu/<b>news</b>-source/your-houston-<b>news</b>/',
      pagemap: [Object]
    },
    {
      kind: 'customsearch#result',
      title: 'Newsday | News Sources | Department of Computer Science ...',
      htmlTitle: 'Newsday | <b>News</b> Sources | Department of Computer Science ...',
      link: 'https://www.cs.columbia.edu/news-source/newsday/',
      displayLink: 'www.cs.columbia.edu',
      snippet: 'Learn more about the COVID-19 Response ... In the News ... New Twitter Feature \n' +
        'Challenges Users to Read Articles Before Sharing (Peter Allen,Daniel Bauer ...',
      htmlSnippet: 'Learn more about the <b>COVID</b>-19 Response ... In the <b>News</b> ... New Twitter Feature <br>\n' +
        'Challenges Users to Read <b>Articles</b> Before Sharing (Peter Allen,Daniel Bauer&nbsp;...',
      cacheId: 'EwzgYOy-S6YJ',
      formattedUrl: 'https://www.cs.columbia.edu/news-source/newsday/',
      htmlFormattedUrl: 'https://www.cs.columbia.edu/<b>news</b>-source/<b>news</b>day/',
      pagemap: [Object]
    },
    {
      kind: 'customsearch#result',
      title: 'Globalization and Offshoring of Software',
      htmlTitle: 'Globalization and Offshoring of Software',
      link: 'https://www.acm.org/binaries/content/assets/public-policy/usacm/intellectual-property/reports-and-white-papers/full_final1.pdf',
      displayLink: 'www.acm.org',
      snippet: 'While articles on offshoring and outsourcing appeared in the media weekly, ... \n' +
        'Source: Compilation from various newspaper articles by Martin Wildemann 2005:\n' +
        ' ...',
      htmlSnippet: 'While <b>articles</b> on offshoring and outsourcing appeared in the media weekly, ... <br>\n' +
        'Source: Compilation from various <b>newspaper articles</b> by Martin Wildemann 2005:<br>\n' +
        '&nbsp;...',
      formattedUrl: 'https://www.acm.org/binaries/content/assets/public.../full_final1.pdf',
      htmlFormattedUrl: 'https://www.acm.org/binaries/content/assets/public.../full_final1.pdf',
      pagemap: [Object],
      mime: 'application/pdf',
      fileFormat: 'PDF/Adobe Acrobat'
    },
    {
      kind: 'customsearch#result',
      title: 'data hub | Department of Computer Science, Columbia University',
      htmlTitle: 'data hub | Department of Computer Science, Columbia University',
      link: 'https://www.cs.columbia.edu/tag/data-hub/',
      displayLink: 'www.cs.columbia.edu',
      snippet: 'For the COVID-19 Hub, CRAC was asked to join a team along with CUIT and \n' +
        'senior leadership across every ... I was interested because of the COVID-related \n' +
        'nature of the project. ... In the News ... New Twitter Feature Challenges Users to \n' +
        'Read Articles Before Sharing (Peter Allen,Daniel Bauer,Augustin Chaintreau) ...',
      htmlSnippet: 'For the <b>COVID</b>-19 Hub, CRAC was asked to join a team along with CUIT and <br>\n' +
        'senior leadership across every ... I was interested because of the <b>COVID</b>-related <br>\n' +
        'nature of the project. ... In the <b>News</b> ... New Twitter Feature Challenges Users to <br>\n' +
        'Read <b>Articles</b> Before Sharing (Peter Allen,Daniel Bauer,Augustin Chaintreau)&nbsp;...',
      cacheId: 'r5LNPkX3GMIJ',
      formattedUrl: 'https://www.cs.columbia.edu/tag/data-hub/',
      htmlFormattedUrl: 'https://www.cs.columbia.edu/tag/data-hub/',
      pagemap: [Object]
    },
    {
      kind: 'customsearch#result',
      title: 'Association for Computing Machinery',
      htmlTitle: 'Association for Computing Machinery',
      link: 'https://www.acm.org/?p=t&offset=45',
      displayLink: 'www.acm.org',
      snippet: 'Read the IFIP news release. ... CRA Surveys Computer Science Faculty, \n' +
        "Department Heads on COVID-19's Impacts ... sensitive interests FB has assigned \n" +
        'them in a Contributed Article in the January 2021 issue of Communications of the \n' +
        'ACM.',
      htmlSnippet: 'Read the IFIP <b>news</b> release. ... CRA Surveys Computer Science Faculty, <br>\n' +
        'Department Heads on <b>COVID</b>-19&#39;s Impacts ... sensitive interests FB has assigned <br>\n' +
        'them in a Contributed <b>Article</b> in the January 2021 issue of Communications of the <br>\n' +
        'ACM.',
      cacheId: '4E0D_zMzG8kJ',
      formattedUrl: 'https://www.acm.org/?p=t&offset=45',
      htmlFormattedUrl: 'https://www.acm.org/?p=t&amp;offset=45',
      pagemap: [Object]
    },
    {
      kind: 'customsearch#result',
      title: 'Revelation and Bible Prophecy',
      htmlTitle: 'Revelation and Bible Prophecy',
      link: 'http://www.cs.ubc.ca/~knorr/public/comparison_of_eschat_models.pdf',
      displayLink: 'www.cs.ubc.ca',
      snippet: '6 days ago ... dispensationalists) have a newspaper in one hand, and a Bible in the other. \n' +
        'Instead ... articles (“a”, “an”, or “the”) present in some of those passages in the \n' +
        'Greek. ... events (e.g., flu outbreaks, COVID-19 coronavirus), corporate.',
      htmlSnippet: '6 days ago <b>...</b> dispensationalists) have a <b>newspaper</b> in one hand, and a Bible in the other. <br>\n' +
        'Instead ... <b>articles</b> (“a”, “an”, or “the”) present in some of those passages in the <br>\n' +
        'Greek. ... events (e.g., flu outbreaks, <b>COVID</b>-19 <b>coronavirus</b>), corporate.',
      formattedUrl: 'www.cs.ubc.ca/~knorr/public/comparison_of_eschat_models.pdf',
      htmlFormattedUrl: 'www.cs.ubc.ca/~knorr/public/comparison_of_eschat_models.pdf',
      pagemap: [Object],
      mime: 'application/pdf',
      fileFormat: 'PDF/Adobe Acrobat'
    }
  ]
}

How to extract all the keys and values from the above object? When I printed res.body['kind'] , it printed only the first key in the object. And when I tried to print res.body['items'] , it threw an error items not defined. How to extract the items list from the object

Edit: I am using unirest node module to get the content from the api:

  var req = unirest(
    "GET",
    "https://www.googleapis.com/customsearch/v1?key=<key here>&cx=<cx here>&q=covid+news+articles&g1=ca"
  );
  req.end(function (res) {
    if (res.error) throw new Error(res.error);

    console.log(res.body);
  });

  });

When I printed res.body , I got the above dictionary. I want to extract all the keys and their respective values from above dictionary using res.body

I got the solution. I used Object.keys(res.body) to get all the keys from the object. I got this output when I printed Object.keys(res.body) :

[ 'kind', 'url', 'queries', 'context', 'searchInformation', 'items' ]

I then printed items in the res.body using res.body.items

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM