wp api – How can I access nested Json data?
Question
I tried to show NYtimes bestseller API’s JSON data.
Here is the JSON
"results": {
"list_name": "Hardcover Fiction",
"list_name_encoded": "hardcover-fiction",
"bestsellers_date": "2022-05-21",
"published_date": "2022-06-05",
"published_date_description": "latest",
"next_published_date": "",
"previous_published_date": "2022-05-29",
"display_name": "Hardcover Fiction",
"normal_list_ends_at": 15,
"updated": "WEEKLY",
"books": [
{
"rank": 1,
"rank_last_week": 0,
"weeks_on_list": 1,
"asterisk": 0,
"dagger": 0,
"primary_isbn10": "1982181656",
"primary_isbn13": "9781982181659",
"publisher": "Atria/Emily Bestler",
"description": "The fifth book in the Terminal List series. James Reece goes after the killer of a Mossad operative attached to the C.I.A.",
"price": "0.00",
"title": "IN THE BLOOD",
"author": "Jack Carr",
"contributor": "by Jack Carr",
"contributor_note": "",
"book_image": "https://storage.googleapis.com/du-prd/books/images/9781982181659.jpg",
"book_image_width": 337,
"book_image_height": 500,
"amazon_product_url": "https://www.amazon.com/dp/1982181656?tag=NYTBSREV-20",
"age_group": "",
"book_review_link": "",
"first_chapter_link": "",
"sunday_review_link": "",
"article_chapter_link": "",
"isbns": [
{ "isbn10": "1982181656", "isbn13": "9781982181659" },
{ "isbn10": "1982181680", "isbn13": "9781982181680" }
],
"buy_links": [
{ "name": "Amazon", "url": "https://www.amazon.com/dp/1982181656?tag=NYTBSREV-20" },
{ "name": "Apple Books", "url": "https://goto.applebooks.apple/9781982181659?at=10lIEQ" },
{ "name": "Barnes and Noble", "url": "https://www.anrdoezrs.net/click-7990613-11819508?url=https%3A%2F%2Fwww.barnesandnoble.com%2Fw%2F%3Fean%3D9781982181659" },
{
"name": "Books-A-Million",
"url": "https://du-gae-books-dot-nyt-du-prd.appspot.com/redirect?url1=https%3A%2F%2Fwww.anrdoezrs.net%2Fclick-7990613-35140%3Furl%3Dhttps%253A%252F%252Fwww.booksamillion.com%252Fp%252FIN%252BTHE%252BBLOOD%252FJack%252BCarr%252F9781982181659&url2=https%3A%2F%2Fwww.anrdoezrs.net%2Fclick-7990613-35140%3Furl%3Dhttps%253A%252F%252Fwww.booksamillion.com%252Fsearch%253Fquery%253DIN%252BTHE%252BBLOOD%252BJack%252BCarr"
},
{
"name": "Bookshop",
"url": "https://du-gae-books-dot-nyt-du-prd.appspot.com/redirect?url1=https%3A%2F%2Fbookshop.org%2Fa%2F3546%2F9781982181659&url2=https%3A%2F%2Fbookshop.org%2Fbooks%3Faffiliate%3D3546%26keywords%3DIN%2BTHE%2BBLOOD"
},
{
"name": "IndieBound",
"url": "https://du-gae-books-dot-nyt-du-prd.appspot.com/redirect?url1=https%3A%2F%2Fwww.indiebound.org%2Fbook%2F9781982181659%3Faff%3DNYT&url2=https%3A%2F%2Fwww.indiebound.org%2Fsearch%2Fbook%3Fkeys%3DIN%2BTHE%2BBLOOD%2BJack%2BCarr%26aff%3DNYT"
}
],
"book_uri": "nyt://book/15be5903-78f0-5d2a-9ca7-f0e468dab5c5"
}
Here’s my PHP code so far and It is working to retrive data from books array.
$requestNY = wp_remote_get( 'https://api.nytimes.com/svc/books/v3/lists/current/hardcover-fiction.json?api-key=xxx' );
$bodyNY = wp_remote_retrieve_body( $requestNY );
$dataNY = json_decode( $bodyNY );
if( ! empty( $dataNY ) ) {
echo '<ul>';
foreach( $dataNY->results->books as $result ) {
echo '<li>';
echo "$result->title by $result->author" ;
echo '<br/> Amazon link: ';
echo '<a href="' . esc_url( $result->amazon_product_url ) . '">' . $result->amazon_product_url . '</a>';
echo'<img src="'.$result->book_image.'" width="150">';
echo '</li>';
}
echo $dataNY->copyright;
echo '</ul>';
}
Now I want to access isbns and buy_links data. How to do that?
0
8 months
2022-05-26T03:44:16-05:00
2022-05-26T03:44:16-05:00 0 Answers
0 views
0
Leave an answer