unable to alert offerid using json object

I am trying to alert offerId but unable to print it tried with json stringify and json.parse but not working, offer id under products should be printed in alert please suggest, if i can alert offerId then problem will get solved.

{
"result": {
    "kind": "content#productsCustomBatchResponse",
    "entries": [
        {
            "kind": "content#productsCustomBatchResponseEntry",
            "batchId": 0,
            "product": {
                "kind": "content#product",
                "id": "online:en:IN:15783",
                "offerId": "15783",
                "contentLanguage": "en",
                "targetCountry": "IN",
                "channel": "online"
            }
        },
        {
            "kind": "content#productsCustomBatchResponseEntry",
            "batchId": 1,
            "product": {
                "kind": "content#product",
                "id": "online:en:IN:15831",
                "offerId": "15831",
                "contentLanguage": "en",
                "targetCountry": "IN",
                "channel": "online"
            }
        },
        {
            "kind": "content#productsCustomBatchResponseEntry",
            "batchId": 2,
            "product": {
                "kind": "content#product",
                "id": "online:en:IN:15846",
                "offerId": "15846",
                "contentLanguage": "en",
                "targetCountry": "IN",
                "channel": "online"
            }
        }
    ]
},
"body": "{\n  \"kind\": \"content#productsCustomBatchResponse\",\n  \"entries\": [\n    {\n      \"kind\": \"content#productsCustomBatchResponseEntry\",\n      \"batchId\": 0,\n      \"product\": {\n        \"kind\": \"content#product\",\n        \"id\": \"online:en:IN:15783\",\n        \"offerId\": \"15783\",\n        \"contentLanguage\": \"en\",\n        \"targetCountry\": \"IN\",\n        \"channel\": \"online\"\n      }\n    },\n    {\n      \"kind\": \"content#productsCustomBatchResponseEntry\",\n      \"batchId\": 1,\n      \"product\": {\n        \"kind\": \"content#product\",\n        \"id\": \"online:en:IN:15831\",\n        \"offerId\": \"15831\",\n        \"contentLanguage\": \"en\",\n        \"targetCountry\": \"IN\",\n        \"channel\": \"online\"\n      }\n    },\n    {\n      \"kind\": \"content#productsCustomBatchResponseEntry\",\n      \"batchId\": 2,\n      \"product\": {\n        \"kind\": \"content#product\",\n        \"id\": \"online:en:IN:15846\",\n        \"offerId\": \"15846\",\n        \"contentLanguage\": \"en\",\n        \"targetCountry\": \"IN\",\n        \"channel\": \"online\"\n      }\n    }\n  ]\n}\n",
"headers": {
    "cache-control": "private",
    "content-encoding": "gzip",
    "content-length": "231",
    "content-type": "application/json; charset=UTF-8",
    "date": "Wed, 04 May 2022 09:52:42 GMT",
    "server": "ESF",
    "vary": "Origin, X-Origin, Referer"
},
"status": 200,
"statusText": null }

Javascript code

function execute() {

return gapi.client.content.products.custombatch(<?php echo json_encode($product_data,JSON_PRETTY_PRINT); ?>).then(function(response) 
{  //not working    alert(response.result[2]);  //alert response.status is working alert(response.status);   });

1 answer

  • answered 2022-05-04 10:35 Ayush Gupta

    //Please Try This
    
      var data =  {
    "result": {
        "kind": "content#productsCustomBatchResponse",
        "entries": [
            {
                "kind": "content#productsCustomBatchResponseEntry",
                "batchId": 0,
                "product": {
                    "kind": "content#product",
                    "id": "online:en:IN:15783",
                    "offerId": "15783",
                    "contentLanguage": "en",
                    "targetCountry": "IN",
                    "channel": "online"
                }
            },
            {
                "kind": "content#productsCustomBatchResponseEntry",
                "batchId": 1,
                "product": {
                    "kind": "content#product",
                    "id": "online:en:IN:15831",
                    "offerId": "15831",
                    "contentLanguage": "en",
                    "targetCountry": "IN",
                    "channel": "online"
                }
            },
            {
                "kind": "content#productsCustomBatchResponseEntry",
                "batchId": 2,
                "product": {
                    "kind": "content#product",
                    "id": "online:en:IN:15846",
                    "offerId": "15846",
                    "contentLanguage": "en",
                    "targetCountry": "IN",
                    "channel": "online"
                }
            }
        ]
    },
    "body": "{\n  \"kind\": \"content#productsCustomBatchResponse\",\n  \"entries\": [\n    {\n      \"kind\": \"content#productsCustomBatchResponseEntry\",\n      \"batchId\": 0,\n      \"product\": {\n        \"kind\": \"content#product\",\n        \"id\": \"online:en:IN:15783\",\n        \"offerId\": \"15783\",\n        \"contentLanguage\": \"en\",\n        \"targetCountry\": \"IN\",\n        \"channel\": \"online\"\n      }\n    },\n    {\n      \"kind\": \"content#productsCustomBatchResponseEntry\",\n      \"batchId\": 1,\n      \"product\": {\n        \"kind\": \"content#product\",\n        \"id\": \"online:en:IN:15831\",\n        \"offerId\": \"15831\",\n        \"contentLanguage\": \"en\",\n        \"targetCountry\": \"IN\",\n        \"channel\": \"online\"\n      }\n    },\n    {\n      \"kind\": \"content#productsCustomBatchResponseEntry\",\n      \"batchId\": 2,\n      \"product\": {\n        \"kind\": \"content#product\",\n        \"id\": \"online:en:IN:15846\",\n        \"offerId\": \"15846\",\n        \"contentLanguage\": \"en\",\n        \"targetCountry\": \"IN\",\n        \"channel\": \"online\"\n      }\n    }\n  ]\n}\n",
    "headers": {
        "cache-control": "private",
        "content-encoding": "gzip",
        "content-length": "231",
        "content-type": "application/json; charset=UTF-8",
        "date": "Wed, 04 May 2022 09:52:42 GMT",
        "server": "ESF",
        "vary": "Origin, X-Origin, Referer"
    },
    "status": 200,
    "statusText": null }
    
    data.result.entries.map(myFunction);
    function myFunction(num) {
     alert(num.product.offerId);
    }
    

How many English words
do you know?
Test your English vocabulary size, and measure
how many words do you know
Online Test
Powered by Examplum