Hey, I’m a bit new to complex JSON data. How could I get the videoID
in this?
{
"kind": "youtube#searchListResponse",
"etag": "TQukOdgHpBrLs5FY_ykhq5ZjbSQ",
"nextPageToken": "CAEQAA",
"regionCode": "US",
"pageInfo": {
"totalResults": 218,
"resultsPerPage": 1
},
"items": [
{
"kind": "youtube#searchResult",
"etag": "Q2esMAnvt-E2-hkHdWwEN3oHBQo",
"id": {
"kind": "youtube#video",
"videoId": "mQhtRm3oniY"
},
"snippet": {
"publishedAt": "2020-06-30T16:03:10Z",
"channelId": "UCoHNPdbSrE2c_g95JgGiBkw",
"title": "Why is my channel called Riverside Rocks?",
"description": "Today is the day you shall all find out... Subscribe to Riverside Rocks! https://bit.ly/RiversideRocks Discord: https://discord.gg/DJKtxbs.",
"thumbnails": {
"default": {
"url": "https://i.ytimg.com/vi/mQhtRm3oniY/default.jpg",
"width": 120,
"height": 90
},
"medium": {
"url": "https://i.ytimg.com/vi/mQhtRm3oniY/mqdefault.jpg",
"width": 320,
"height": 180
},
"high": {
"url": "https://i.ytimg.com/vi/mQhtRm3oniY/hqdefault.jpg",
"width": 480,
"height": 360
}
},
"channelTitle": "Riverside Rocks",
"liveBroadcastContent": "none",
"publishTime": "2020-06-30T16:03:10Z"
}
}
]
}
A JSON is structured like this :
var somevariable = {a:"1",b:"2",c:"3" }
console.log(somevariable.a) //1
but you can also “put a json in a json” :
var somevariable = {a: {z:"what",x:"letter"} ,b:"hey"}
console.log(somevariable.a.x) //letter
and also arrays, and a “json inside an array” :
var somevariable = {a: ["how","are","you"], b: [ {film : "James Doe", producer : "Williams John"}, {film : "Trek in the Stars",producer:"Hedgehog Ford"} ] }
console.log(somevariable.a[2]) // you
console.log(somevariable.b[0].film+" was made by "+ somevariable.b[0].producer) //James Doe was made by Williams John
2 Likes
I was just wondering, is there a way to do this in PHP?
JSON - JavaScript Object Notation - a.k.a. no
Edit: @RiversideRocks turns out it’s a yes
https://www.w3schools.com/js/js_json_php.asp
I have worked with JSON server side but never on this level.
Fun Fact: PHP stood for Personal Home Page, today it stands for PHP: Hypertext Processor. So I guess the full thing is Personal Home Page: Hypertext Processor. So PHPHP?
Does JSON really differ while using PHP?
Not so sure. Even languages like Swift can read and write to JSON (with some work obviously)
3 Likes
It was probably ported over the years to other languages as the french wikipedia states that most languages have access to JSON libraries.
Hey folks,
JSON is definitely meant to be for interchanging data between different platforms and languages even though it has JS in the name
You can use it anywhere, like XML.
In PHP, first you’re going to “deserialize” the JSON data into a PHP associative array with json_decode.
Then since there’s only one item
in the items
array, you can get the videoId
like this:
$searchResponse = json_decode($myJson);
$videoId = $searchResponse['items'][0]['id']['videoId'];
Untested, so you may have to fiddle with it.
Hope it helps!
4 Likes
I appreciate your good answer, including the explanations you have provided! 

2 Likes
Thanks! I’m currently tweaking it, but it seems to be what I need.
1 Like
Sorry, I’m trying not to be spoonfed, but I’m just confused because it looks very logical like it would work, but I keep getting an error that I believe means it can’t find the object?
At some point along the line, it’s decided that the thing you’re accessing is null so it can’t go the next step.
One tip is to use var_dump
to see what is in your $searchResponse
(or whatever you called the object) https://www.php.net/manual/en/function.var-dump.php
I’d also break it down like this to see what’s going on:
$items= $searchResponse['items'];
$firstItem= $items[0];
$id= $firstItem['id'];
Then you can see which line it fails on, which will tell you what is actually null.