Extracting data from api

{
“status”: “OK”,
“copyright”: “Copyright (c) 2023 The New York Times Company. All Rights Reserved.”,
“num_results”: 20,
“results”: [
{
“uri”: “nyt://article/fc974ed3-5b15-570f-b9d6-2accc033a5b1”,
“url”: “https://www.nytimes.com/2023/11/18/technology/sam-altman-openai-board.html”,
“id”: 100000009187785,
“asset_id”: 100000009187785,
“source”: “New York Times”,
“published_date”: “2023-11-18”,
“updated”: “2023-11-18 23:14:41”,
“section”: “Technology”,
“subsection”: “”,
“nytdsection”: “technology”,
“adx_keywords”: “Start-ups;Appointments and Executive Changes;Artificial Intelligence;Boards of Directors;Altman, Samuel H;Brockman, Greg;Microsoft Corp;OpenAI Labs;Sequoia Capital;Thrive Capital;San Francisco (Calif)”,
“column”: null,
“byline”: “By Cade Metz, Mike Isaac and Erin Griffith”,
“type”: “Article”,
“title”: “Sam Altman Is Said to Be Discussing Return to OpenAI With Company’s Board”,
“abstract”: “Mr. Altman was forced out of the artificial intelligence start-up on Friday, leading to an outcry from his supporters and the company’s investors.”,
“des_facet”: [
“Start-ups”,
“Appointments and Executive Changes”,
“Artificial Intelligence”,
“Boards of Directors”
],
“org_facet”: [
“Microsoft Corp”,
“OpenAI Labs”,
“Sequoia Capital”,
“Thrive Capital”
],
“per_facet”: [
“Altman, Samuel H”,
“Brockman, Greg”
],
“geo_facet”: [
“San Francisco (Calif)”
],
“media”: [
{
“type”: “image”,
“subtype”: “photo”,
“caption”: “The new talks between Sam Altman and OpenAI’s board were the latest twist in a fast-moving drama at what is perhaps the world’s highest-profile artificial intelligence company.”,
“copyright”: “Jim Wilson/The New York Times”,
“approved_for_syndication”: 1,
“media-metadata”: [
{
“url”: “https://static01.nyt.com/images/2023/11/18/multimedia/18openai-altman-1-whzc/18openai-altman-1-whzc-thumbStandard.jpg”,
“format”: “Standard Thumbnail”,
“height”: 75,
“width”: 75
},
{
“url”: “https://static01.nyt.com/images/2023/11/18/multimedia/18openai-altman-1-whzc/18openai-altman-1-whzc-mediumThreeByTwo210.jpg”,
“format”: “mediumThreeByTwo210”,
“height”: 140,
“width”: 210
},
{
“url”: “https://static01.nyt.com/images/2023/11/18/multimedia/18openai-altman-1-whzc/18openai-altman-1-whzc-mediumThreeByTwo440.jpg”,
“format”: “mediumThreeByTwo440”,
“height”: 293,
“width”: 440
}
]
}
],
“eta_id”: 0
},

Hi i am striggling to extract the image url data from this new yorks times api that im practicing with, can anyone help?

1 Like

So, I’ve spent some time and this is what I can do for you,

const jsonData = { "status": "OK", "copyright": "Copyright (c) 2023 The New York Times Company. All Rights Reserved.", "num_results": 20, "results": [ { "uri": "nyt://article/fc974ed3-5b15-570f-b9d6-2accc033a5b1", "url": "https://www.nytimes.com/2023/11/18/technology/sam-altman-openai-board.html", "id": 100000009187785, "asset_id": 100000009187785, "source": "New York Times", "published_date": "2023-11-18", "updated": "2023-11-18 23:14:41", "section": "Technology", "subsection": "", "nytdsection": "technology", "adx_keywords": "Start-ups;Appointments and Executive Changes;Artificial Intelligence;Boards of Directors;Altman, Samuel H;Brockman, Greg;Microsoft Corp;OpenAI Labs;Sequoia Capital;Thrive Capital;San Francisco (Calif)", "column": null, "byline": "By Cade Metz, Mike Isaac and Erin Griffith", "type": "Article", "title": "Sam Altman Is Said to Be Discussing Return to OpenAI With Company’s Board", "abstract": "Mr. Altman was forced out of the artificial intelligence start-up on Friday, leading to an outcry from his supporters and the company’s investors.", "des_facet": [ "Start-ups", "Appointments and Executive Changes", "Artificial Intelligence", "Boards of Directors" ], "org_facet": [ "Microsoft Corp", "OpenAI Labs", "Sequoia Capital", "Thrive Capital" ], "per_facet": [ "Altman, Samuel H", "Brockman, Greg" ], "geo_facet": [ "San Francisco (Calif)" ], "media": [ { "type": "image", "subtype": "photo", "caption": "The new talks between Sam Altman and OpenAI’s board were the latest twist in a fast-moving drama at what is perhaps the world’s highest-profile artificial intelligence company.", "copyright": "Jim Wilson/The New York Times", "approved_for_syndication": 1, "media-metadata": [ { "url": "https://static01.nyt.com/images/2023/11/18/multimedia/18openai-altman-1-whzc/18openai-altman-1-whzc-thumbStandard.jpg", "format": "Standard Thumbnail", "height": 75, "width": 75 }, { "url": "https://static01.nyt.com/images/2023/11/18/multimedia/18openai-altman-1-whzc/18openai-altman-1-whzc-mediumThreeByTwo210.jpg", "format": "mediumThreeByTwo210", "height": 140, "width": 210 }, { "url": "https://static01.nyt.com/images/2023/11/18/multimedia/18openai-altman-1-whzc/18openai-altman-1-whzc-mediumThreeByTwo440.jpg", "format": "mediumThreeByTwo440", "height": 293, "width": 440 } ] } ], "eta_id": 0 } ] } const imageURL = jsonData.results[0].media[0]['media-metadata'][0].url; console.log(imageURL);

If you wanna change the photo, just change the last index in 1 or 2. Hope this helps

oh wow thank you so much, how long did it take you? i also managed to figure it out but it took me till 1am in the morning!

thank you for your time much appreciated.

1 Like

Not so much, around 15 minutes, but I’ve dealt with JSON files in python before, so I just needed to see how to use that in JS. I’m always happy to help someone, good luck with your coding journey