'how to extract product name from HTML in website in postman

I am trying to verify if products has been added in mobile webview checkout. I tried Cheerios but not been able to get much out of it.

In the HTML response I can see below script which has products information and I would like to get the name Short Sleeve Polo of the product from this script inside ecommerce array. Thanks in advance

<script>
    pageContext = {
       "currentPage":"shipping",
       "title":"Checkout",
       "type":"checkout",
       "ns":"checkout",
       "analytics":{
          "user":{
             "customerType":"New",
             "ecomStore":"Demandware",
             "userType":"regular user",
             "gender":"Female",
             "hasTransacted":false,
             "userEmailAddress":"[email protected]",
             "userId":"W05dds015457",
             "socialNetwork":""
          },
          "basket":{
             "ecommerce":[
                {
                   "name":"Short Sleeve Polo Shirt",
                   "id":"AA1s5083",
                   "variationid":"AA15s083001",
                   "isEdits":null,
                   "price":"10.00",
                   "brand":"Test",
                   "category":"variation-masters",
                   "variant":"Fresh Blue|3/4",
                   "quantity":1,
                   "position":0
                },
                {
                   "name":"Limited Edition Short Sleeve Polo Shirt",
                   "id":"AA15080",
                   "variationid":"AA15080007",
                   "isEdits":null,
                   "price":"12.00",
                   "brand":"Test",
                   "category":"kidswear",
                   "variant":"Dusky Jade|3/4",
                   "quantity":1,
                   "position":1
                }
             ]
          },
          "page":{
             "currencyCode":"GBP",
             "pageCategory":"shipping",
             "subCategory":"shipping",
             "pageName":"SiteGenesis Checkout",
             "pageDesc":"",
             "pageLocale":"en_GB",
             "pageCurrency":"GBP",
             "pageType":"shipping",
             "user":"regular user"
          }
       },
       "billingAllowed":false
    };
</script>


Solution 1:[1]

One way you can do it is by getting the text of the script, targeting the JSON of the variable, parsing it, then finally targetting the values you want just like a normal array/object.

const cheerio = require('cheerio');
const $ = cheerio.load(html); // your html

const text = $('script')[0].text(); // TODO there might be multiple script tags

// get variable pageContext in the text
const matches = text.match(/pageContext = ([^;]*);/);

// removes any possible '\n' that show up and assign
// matches[1] (which now contains the json text) to a variable
var json = matches[1].replace(/\n/gi, '');

// parse the JSON that has been collected
var pageContext = JSON.parse(json);

// get both names from the ecommerce array items
var value1 = pageContext.analytics.basket.ecommerce[0].name;
var value2 = pageContext.analytics.basket.ecommerce[1].name;

Ref: Accessing variable in script tag with jQuery/cheerio

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 JM-AGMS