-2

Using dot notation to access a JSON response is returning different values than what the API response is saying in my browser

I tried to access a JSON response from an axios request like so:

const response = await axios.get('https://store-site-backend-static.ak.epicgames.com/freeGamesPromotions?locale=en-US&country=US&allowCountries=US');

console.log(response.data.data.Catalog.searchStore.elements.promotions)

Instead of getting a response something similar to this in JSON:

{
    "promotions": {
        "promotionalOffers": [
            {
                "promotionalOffers": [
                    {
                        "startDate": "2023-02-02T16:00:00.000Z",
                        "endDate": "2023-02-09T16:00:00.000Z",
                        "discountSetting": {
                            "discountType": "PERCENTAGE",
                            "discountPercentage": 0
                        }
                    }
                ]
            }
        ],
        "upcomingPromotionalOffers": []
    }
}

I simply get this from the console log:

undefined
undefined
undefined
undefined
undefined

I might not be using dot notation to access it correctly but I have no idea. You can view the JSON response from the browser here: https://store-site-backend-static.ak.epicgames.com/freeGamesPromotions?locale=en-US&country=US&allowCountries=US

vishal-wadhwa
  • 1,021
  • 12
  • 18
Andrew
  • 13
  • 6

1 Answers1

0

#1 Getting data from the server by axios. The elements start [ and end ] is an array data.

enter image description here

(* I copy/paste from Browser to VS code after access URL, saved JSON data) VS code can expand/collapse data by clicking down arrow.

#2 Filter only promotions It is one of Key values of array elements

enter image description here

You can filter by Array map()

Simple example for getting title only

const titles = elements.map(item => { return { title : item.title }  } )
console.log(JSON.stringify(titles, null, 4))
[
    {
        "title": "Borderlands 3 Season Pass"
    },
    {
        "title": "City of Gangsters"
    },
    {
        "title": "Recipe for Disaster"
    },
    {
        "title": "Dishonored®: Death of the Outsider™"
    },
    {
        "title": "Dishonored - Definitive Edition"
    }
]

So this code will works for getting promotions

const axios = require('axios')

const getPromotions = async (data) => {
    try {
        const response = await axios.get('https://store-site-backend-static.ak.epicgames.com/freeGamesPromotions?locale=en-US&country=US&allowCountries=US');
        return Promise.resolve(response.data)
    } catch (error) {
        return Promise.reject(error)
    }
};

getPromotions()
    .then(result => {
        const promotions = result.data.Catalog.searchStore.elements.map(item => { return { promotions : item.promotions }  } )
        console.log(JSON.stringify(promotions, null, 4))
    })
    .catch(error => {
        console.log(error.message)
    });

Result

$ node get-data.js
[
    {
        "promotions": {
            "promotionalOffers": [
                {
                    "promotionalOffers": [
                        {
                            "startDate": "2023-01-26T16:00:00.000Z",
                            "endDate": "2023-02-09T16:00:00.000Z",
                            "discountSetting": {
                                "discountType": "PERCENTAGE",
                                "discountPercentage": 30
                            }
                        },
                        {
                            "startDate": "2023-01-26T16:00:00.000Z",
                            "endDate": "2023-02-09T16:00:00.000Z",
                            "discountSetting": {
                                "discountType": "PERCENTAGE",
                                "discountPercentage": 30
                            }
                        }
                    ]
                }
            ],
            "upcomingPromotionalOffers": []
        }
    },
    {
        "promotions": {
            "promotionalOffers": [
                {
                    "promotionalOffers": [
                        {
                            "startDate": "2023-02-02T16:00:00.000Z",
                            "endDate": "2023-02-09T16:00:00.000Z",
                            "discountSetting": {
                                "discountType": "PERCENTAGE",
                                "discountPercentage": 0
                            }
                        }
                    ]
                }
            ],
            "upcomingPromotionalOffers": []
        }
    },
    {
        "promotions": {
            "promotionalOffers": [],
            "upcomingPromotionalOffers": [
                {
                    "promotionalOffers": [
                        {
                            "startDate": "2023-02-09T16:00:00.000Z",
                            "endDate": "2023-02-16T16:00:00.000Z",
                            "discountSetting": {
                                "discountType": "PERCENTAGE",
                                "discountPercentage": 0
                            }
                        }
                    ]
                }
            ]
        }
    },
    {
        "promotions": {
            "promotionalOffers": [
                {
                    "promotionalOffers": [
                        {
                            "startDate": "2023-02-02T16:00:00.000Z",
                            "endDate": "2023-02-09T16:00:00.000Z",
                            "discountSetting": {
                                "discountType": "PERCENTAGE",
                                "discountPercentage": 0
                            }
                        }
                    ]
                }
            ],
            "upcomingPromotionalOffers": []
        }
    },
    {
        "promotions": null
    }
]
Bench Vue
  • 5,257
  • 2
  • 10
  • 14