Having trouble parsing data from web API (NewsAPI)

Hi,

I’m trying to fetch the title of a BBC article using the NewsAPI and it doesn’t work.

Here’s the URL I’m using: https://newsapi.org/v2/everything?domains=bbc.co.uk&pageSize=1&apiKey=xyz

Here’s the JSON:

{
   "status":"ok",
   "totalResults":4848,
   "articles":[
      {
         "source":{
            "id":"bbc-news",
            "name":"BBC News"
         },
         "author":"BBC News",
         "title":"Coronavirus: Twins returning to nursing at Eastbourne hospital",
         "description":"Carole and Lynda Heather worked together as nurses for more than 40 years and retired on the same day.",
         "url":"https://www.bbc.co.uk/news/av/uk-england-sussex-52633499/coronavirus-twins-returning-to-nursing-at-eastbourne-hospital",
         "urlToImage":"https://ichef.bbci.co.uk/news/1024/branded_news/9C9D/production/_112239004_p08cxx2h.jpg",
         "publishedAt":"2020-05-12T12:07:11Z",
         "content":null
      }
   ]
}

And here’s my code:

Any help would be much appreciated :grinning:

There seems to be some difficulties with managing a Json response directly from the source. Try saving the api response to a variable (named rawData for example). Then instead of get object from JSON response, try get object from JSON rawData

Hi

this is what has worked for me in the past. Check the documentation for your API just to be sure you are sending the right headers. i think you need to setup your header…
i think you need something closer to this:
get_format

articls is an array. you need to know which object you are calling.
also, the JSON you shared is poorly formatted and wont work to use as an example. here is what i get when i look for UK newsf from the exact same API.

{
	"status": "ok",
	"totalResults": 11,
	"articles": [
		{
			"source": {
				"id": null,
				"name": "Liverpoolecho.co.uk"
			},
			"author": "Paul Gorst",
			"title": "Jurgen Klopp was given three clues Liverpool were on the brink of glory - Liverpool Echo",
			"description": "Whether football resumes or not in this country, the only thing denying Liverpool the title now is an unspeakable collapse in the final nine games",
			"url": "https://www.liverpoolecho.co.uk/sport/football/players-golden-touch-led-liverpool-18237799",
			"urlToImage": "https://i2-prod.liverpoolecho.co.uk/incoming/article18237776.ece/ALTERNATES/s1200/1_Golden-touch.jpg",
			"publishedAt": "2020-05-13T00:06:44Z",
			"content": "As the Liverpool players strolled around Anfield for the final time that season, their efforts were given the rich applause they deserved.\r\nThere would be no Premier League title to mark a campaign that saw just one defeat in 38, but Reds fans were firm in th… [+4175 chars]"
		},
		{
			"source": {
				"id": null,
				"name": "Mirror.co.uk"
			},
			"author": "Jake Polden",
			"title": "Per Mertesacker says he'll give Mikel Arteta 'selection headaches' with academy quality - Mirror Online",
			"description": "Arsenal's academy manager Per Mertesacker says it's his job to give Gunners boss Mikel Arteta a selection headache by preparing young players for first team football",
			"url": "https://www.mirror.co.uk/sport/football/news/per-mertesacker-says-hell-give-22017439",
			"urlToImage": "https://i2-prod.mirror.co.uk/incoming/article21738554.ece/ALTERNATES/s1200/0_Arsenal-Training-Session.jpg",
			"publishedAt": "2020-05-12T22:20:13Z",
			"content": "The Arsenal academy operates at such a high standard that Mikel Arteta must be thinking \"What's going on?\"\r\n That's according to Arsenal's academy manager Per Mertesacker, who says it's his job to give the Gunners boss selection headaches.\r\nIn the past few ye… [+1893 chars]"
		},
		{
			"source": {
				"id": null,
				"name": "Football.london"
			},
			"author": "Tashan Deniran-Alleyne",
			"title": "Arsenal fans have spotted a clue over Pierre-Emerick Aubameyang's new contract announcement - Football.London",
			"description": "Latest Arsenal news includes fan speculation regarding Pierre-Emerick Aubameyang's future, the striker's contract at the Emirates Stadium expires at the end of next season",
			"url": "https://www.football.london/arsenal-fc/news/aubameyang-new-arsenal-contract-announcement-18241899",
			"urlToImage": "https://i2-prod.football.london/incoming/article17885171.ece/ALTERNATES/s1200/1_Aubameyang-4.jpg",
			"publishedAt": "2020-05-12T22:11:31Z",
			"content": "Some Arsenal fans are convinced that Pierre-Emerick Aubameyang will commit his future to the club.\r\nThe Gabon international's future at the Emirates Stadium is uncertain, with his contract expiring at the end of next season and he's also being linked with mov… [+1784 chars]"
		},
		{
			"source": {
				"id": null,
				"name": "Liverpoolecho.co.uk"
			},
			"author": "Josh Williams",
			"title": "Liverpool are set to lose player with one of football's most underrated qualities - Liverpool Echo",
			"description": "Assessing Liverpool's most two-footed player and why it's such a unique quality",
			"url": "https://www.liverpoolecho.co.uk/sport/football/football-news/liverpool-set-lose-player-one-18240353",
			"urlToImage": "https://i2-prod.liverpoolecho.co.uk/incoming/article18124152.ece/ALTERNATES/s1200/0_GettyImages-1165140854.jpg",
			"publishedAt": "2020-05-12T21:19:58Z",
			"content": "There are various footballing qualities that are highly rated inside the game.\r\nIf a player is good at shooting like Sadio Mane, if he's fast like Mohamed Salah or if he's a leader like Jordan Henderson, for example - those traits tend to receive plenty of pu… [+3407 chars]"
		},
		{
			"source": {
				"id": null,
				"name": "Mirror.co.uk"
			},
			"author": "Jake Polden",
			"title": "Dimitar Berbatov believes three Man Utd stars would get in team ahead of Jadon Sancho - Mirror Online",
			"description": "Dimitar Berbatov, who spent four years at Old Trafford, believes Anthony Martial, Marcus Rashford and Daniel James would make first-team football difficult for Sancho",
			"url": "https://www.mirror.co.uk/sport/football/news/dimitar-berbatov-believes-three-man-22016865",
			"urlToImage": "https://i2-prod.mirror.co.uk/incoming/article21943238.ece/ALTERNATES/s1200/0_Champions-League-Round-of-16-First-Leg-Tottenham-Hotspur-v-Borussia-Dortmund.jpg",
			"publishedAt": "2020-05-12T18:40:08Z",
			"content": "Dimitar Berbatov doesn't believe Jadon Sancho should join Manchester United just yet because he wouldn't walk into the first team.\r\nThe Bulgarian, who spent four years at Old Trafford, believes Anthony Martial, Marcus Rashford and Daniel James would make firs… [+1793 chars]"
		},
		{
			"source": {
				"id": null,
				"name": "Football.london"
			},
			"author": "Alasdair Gold",
			"title": "Danny Rose opens up on wanting to quit Tottenham and how Pochettino told him to treat Messi - Football.London",
			"description": "The Tottenham Hotspur left-back, currently on loan at Newcastle United, has been talking frankly about moments in his career",
			"url": "https://www.football.london/tottenham-hotspur-fc/players/danny-rose-leave-tottenham-messi-18236535",
			"urlToImage": "https://i2-prod.football.london/incoming/article17663964.ece/ALTERNATES/s1200/0_GettyImages-956545458.jpg",
			"publishedAt": "2020-05-12T08:29:16Z",
			"content": "Danny Rose has been speaking about the moment that as a teenager at Tottenham Hotspur he wanted to quit the club and football and wanting to leave again later in his career.\r\nRose joined Spurs from Leeds as a 17-year-old and attacking midfielder, only to be s… [+5619 chars]"
		},
		{
			"source": {
				"id": null,
				"name": "Skysports.com"
			},
			"author": "Sky Sports",
			"title": "Christian Pulisic raring to go for Chelsea once football returns after recovering from tendon injury - Sky Sports",
			"description": "Christian Pulisic says the tendon injury that kept him out for three months was more serious than expected but he's now fully recovered and ready to return to football.",
			"url": "https://www.skysports.com/football/news/11668/11986758/christian-pulisic-raring-to-go-for-chelsea-once-football-returns-after-recovering-from-tendon-injury",
			"urlToImage": "https://e1.365dm.com/19/11/1600x900/skysports-christian-pulisic_4833874.jpg",
			"publishedAt": "2020-05-12T06:21:38Z",
			"content": "Christian Pulisic says the tendon injury that kept the Chelsea winger out for three months was more serious than expected but he's now fully recovered and ready to return to football. \r\nThe USA captain has not played since New Year's Day at Brighton and his r… [+2440 chars]"
		},
		{
			"source": {
				"id": null,
				"name": "67hailhail.com"
			},
			"author": "John McGinley",
			"title": "Charlie Adam laughably claims Celtic have been ‘throwing little digs in’ to ongoing SPFL debate - 67 Hail Hail - Celtic FC News",
			"description": "Former Rangers midfielder Charlie Adam has claimed Celtic have been ‘throwing little digs in’ to the ongoing drama engulfing Scottish football, speaking on today’s BBC Sportsound podcast. As we know, the Ibrox side and others have engaged in a very public war…",
			"url": "https://www.67hailhail.com/news/charlie-adam-laughably-claims-celtic-have-been-throwing-little-digs-in-to-ongoing-spfl-debate/",
			"urlToImage": "https://www.67hailhail.com/static/uploads/18/2019/05/GettyImages-1136994247-1024x683.jpg",
			"publishedAt": "2020-05-11T18:55:46Z",
			"content": "Former Rangers midfielder Charlie Adam has claimed Celtic have been ‘throwing little digs in’ to the ongoing drama engulfing Scottish football, speaking on today’s BBC Sportsound podcast.\r\nAs we know, the Ibrox side and others have engaged in a very public wa… [+2200 chars]"
		},
		{
			"source": {
				"id": null,
				"name": "The72.co.uk"
			},
			"author": null,
			"title": "Leeds United biggest hope yet that promotion is an option - latest - The 72 - We Love the Football League",
			"description": "Leeds United sit pretty and on the verge of another promotion tilt. After the suspension of football that was thrown into some doubt. Here hope emerges.",
			"url": "https://the72.co.uk/164884/leeds-united-biggest-hope-yet-that-promotion-is-an-option-latest/",
			"urlToImage": "https://cdn.readeverything.co/wp-content/uploads/sites/111/2020/05/1208890790-1-scaled.jpg",
			"publishedAt": "2020-05-11T18:44:52Z",
			"content": "‘Don’t you know pump it up, the Whites are going up’ was a chant from the terraces by Leeds United fans that roared around every Championship ground this season. When football was suspended, the Whites were on a five-game winning streak without conceding a si… [+2732 chars]"
		},
		{
			"source": {
				"id": null,
				"name": "Youtube.com"
			},
			"author": null,
			"title": "“No Sport Until 1st June” Reaction | Should We Just Void The Season And Hand Liverpool The Title Now - 90min Football",
			"description": "The UK Government have said the earliest sport and therefore the Premier League could return is June 1st. Is there enough time to finish the 2019/20 Premier ...",
			"url": "https://www.youtube.com/watch?v=kozEPGJjKLk",
			"urlToImage": "https://i.ytimg.com/vi/kozEPGJjKLk/maxresdefault.jpg",
			"publishedAt": "2020-05-11T16:54:07Z",
			"content": "The UK Government have said the earliest sport and therefore the Premier League could return is June 1st. Is there enough time to finish the 2019/20 Premier League season, or should we void it. Do Liverpool deserve to be crowned Premier League Champions? Will… [+128 chars]"
		},
		{
			"source": {
				"id": null,
				"name": "Theguardian.com"
			},
			"author": "Luke McLaughlin",
			"title": "Football quiz: how much do you know about the Bundesliga? - The Guardian",
			"description": "Football fans in Germany are expecting to see some action soon. How much do you know about the country’s top tier?",
			"url": "https://www.theguardian.com/football/2020/may/11/football-quiz-how-much-bundesliga-germany",
			"urlToImage": "https://i.guim.co.uk/img/media/d3f622038f7429b11d40b0ad482e3218ec622c91/0_229_3460_2076/master/3460.jpg?width=1200&height=630&quality=85&auto=format&fit=crop&overlay-align=bottom%2Cleft&overlay-width=100p&overlay-base64=L2ltZy9zdGF0aWMvb3ZlcmxheXMvdGctZGVmYXVsdC5wbmc&enable=upscale&s=2a5935eb3f70979ffd4dacbee155c0f6",
			"publishedAt": "2020-05-11T10:24:00Z",
			"content": "Football fans in Germany are expecting to see some action soon. How much do you know about the countrys top tier?"
		}
	]
}

this is how i am using this api
articles is a list. you will want the item number 1 from articles in your case if that’s the only item returned

1 Like

Thank you so much for your responses @jared and @sleon003x2rq. Fair point about sharing properly formatted JSON. Will try your suggestions and get back to you.

1 Like

Oh I see, nvm ignore my comment. didn’t mean to confuse you. I had been working with apis requiring auth headers

@jared your example was so awesome but unfortunately it’s beyond my skill level at this time. What I’m trying to build is far simpler (fetch one article headline using the Everything endpoint). I tried to incorporate your feedback about article being an array but it still doesn’t work. Any thoughts?

Link to Thunkable project here.