
How to get data from JSON file? Simplest way get & upload data.
Today, I will show you how to get data from a JSON file. Then we learn how to upload them to the server through various processes.
It’s very simple and tricky. You can quickly upload data from any JSON file this way. I have uploaded 1 lakh+ data instantly using this process.
Example json
Note: JSON file format must be files.json
[
{ "name": "Physics", "slug": "physics-101" },
{ "name": "Math", "slug": "math-101" },
{ "name": "Chemistry", "slug": "chemistry-101" }
]PHP code and JSON file picker
<?php
// Database connection
$con = new mysqli("localhost", "root", "", db_name");
if ($con->connect_errno) {
die("Failed to connect: " . $con->connect_error);
}
/ Checking json file have or not
if (isset($_FILES['jsonFile'])) {
$jsonContent = file_get_contents($_FILES['jsonFile']['tmp_name']);
$data = json_decode($jsonContent, true);
if (!$data) {
die("Invalid JSON file!");
}
$updated = 0;
foreach ($data as $item) {
$name = $con->real_escape_string($item['name']);
$slug = $con->real_escape_string($item['slug']);
// If match then update
$sql = "UPDATE mcq_tbl SET category = '$slug' WHERE subcategory = '$name'";
if ($con->query($sql) && $con->affected_rows > 0) {
$updated += $con->affected_rows;
}
}
echo "Update Summary<br>";
echo "Total Updated Rows: <b>$updated</b>";
}
?>Below, I’ve created a simple HTML form. I am trying to present it in a simple way. You can make it more attractive using CSS according to your needs.
<!DOCTYPE html>
<html>
<head>
<title>Category Updater</title>
</head>
<body>
<h2>Upload JSON File</h2>
<form method="post" enctype="multipart/form-data">
<input type="file" name="jsonFile" accept=".json" required>
<button type="submit">Upload & Update</button>
</form>
</body>
</html>







