Confused on Reddit Json

Hey Guys!

I’m just learning Kotlin after I went through a developement course on java on android (I’ve also done javeFX before that) and I’m up to json implementation with database and paging through a network.

I know my languages and how json works (if not please tell me) but I’ve come across on how to pull database data into a json_encode and go from there, however it’s in php.

What I’ve looked at is that reddit get’s it’s json data from a json file, and it updates instantly. This is either it has listeners when new information has been submitted and creates and overwrites the json file from other code, or, I am missing something.

Here’s an overview of what I’m looking at:

Reddit Json url API
https://www.reddit.com/r/androiddev/hot.json
See that it’s a json file?

My url PHP API
http://json.test/data.php
On a local test server

PHP API Code

<?php
	//database constants
	define('DB_HOST', 'localhost');
	define('DB_USER', 'root');
	define('DB_PASS', '');
	define('DB_NAME', 'test');
	
	//connecting to database and getting the connection object
	$conn = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);
	
	//Checking if any error occured while connecting
	if (mysqli_connect_errno()) {
		echo "Failed to connect to MySQL: " . mysqli_connect_error();
		die();
	}
	
	//creating a query
	$stmt = $conn->prepare("SELECT id, user FROM names LIMIT 2");
	
	//executing the query 
	$stmt->execute();
	
	//binding results to the query 
	$stmt->bind_result($id, $user);
	$data = array(); 
	
	//traversing through all the result 
	while($stmt->fetch()) {
		$temp = array();
		$temp['id'] = $id; 
		$temp['user'] = $user; 
		array_push($data, $temp);
	}
	
	//displaying the result in json format
	header("Content-Type: application/json; charset=UTF-8");
	echo json_encode(array("data"=>$data));
?>

See that the browser recognizes it as json data

{
  "data": [
    {
      "id": 1,
      "user": "John"
    },
    {
      "id": 2,
      "user": "Mary"
    }
  ]
}

Google Fu has failed me for the last week now, I’m still boggling my mind around this concept.

It’s most likely not a file at all.

Its an API endpoint which returns JSON in the response. Specifically, response.data.

If you would wish to test API’s, the easiest noob friendly way is to use Postman.

1 Like

Thanks for replying, but I’m still confused, this is using some sort of library from Postman that I don’t think I can host on my dedicated server.

It does say it’s using Javascript though, I’ve tried to make sense of how this works, however, looking for “Json response.data” yields no favorable results. Looking for “API endpoint” just gives me REST descriptions and services that I’m not looking for.

Again, thank you for replying, but this just made me even more confused :frowning:

Postman is GUI to help you test api endpoints. The same thing can be done from cli via curl.

You need to understand how REST works.

Basically, when you visit a website, you go to a specific endpoint, website.com and send a GET request to it. The webserver will respond to GET requests at the specified endpoint / with data.

Since its the web root is sends the index file.

When you went to that endpoint up earlier, it responded to you.
It send out a response, and in the response object was a data object which is what you were looking for.

If you wish to do anything meaningful with that data then you would usually push that data object into an array.

Here, check this out.

That’s the server file for my website. So when people go to the webroot it responds by sending them the client fontend code.

If you would like to learn more, watch this guys videos. he’s fantastic.

2 Likes

Thank you!

Honestly I’ve been programming for 9 years with multiple languages and it’s a first to actually gain a concept of REST, lol.

I will watch these videos and let you know how I do!

1 Like

Ok, after I got some time I completed the playlist and followed through, had one or two depreciated misgivings but all good.

Kinda interested about the endpoints since when you compare it to the reddit one, it outputs an actual .json extension whereas doing it from netninja’s tutorial it’s just an endpoint that can be interpreted from anything.

Doing some GoogleFu again to find an answer to this but nothing really comes up, just some header declarations and php scripting which I’ve already been through.

Any ideas?

EDIT: Welp, I just answered my own question.