Skip to content

Instantly share code, notes, and snippets.

@YasserElgammal
Created February 18, 2024 21:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save YasserElgammal/409930790a93725b93e7fccc1735bacc to your computer and use it in GitHub Desktop.
Save YasserElgammal/409930790a93725b93e7fccc1735bacc to your computer and use it in GitHub Desktop.
This file can compare between two paginated json and identify different between total_items returned in pagination result
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>JSON Comparator</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css">
</head>
<body class="container mt-5">
<h1 class="mb-4">JSON Comparator</h1>
<form method="post">
<div class="form-group">
<label for="json1">JSON 1:</label>
<textarea id="json1" name="json1" class="form-control" rows="5" required></textarea>
</div>
<div class="form-group">
<label for="json2">JSON 2:</label>
<textarea id="json2" name="json2" class="form-control" rows="5" required></textarea>
</div>
<div class="form-group">
<label for="key">Key for Comparison (e.g., packages):</label>
<input type="text" id="key" name="key" class="form-control" required>
</div>
<button type="submit" class="btn btn-primary">Compare JSON</button>
</form>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$json1 = json_decode($_POST["json1"], true);
$json2 = json_decode($_POST["json2"], true);
$key = $_POST["key"];
if ($json1 === null || $json2 === null) {
echo "<p class='mt-3 text-danger'>Invalid JSON input. Please enter valid JSON.</p>";
} else {
// Compare JSON based on specified key and "total_items" in pagination
$differences = compareJson($json1, $json2, $key);
// Output the differences
echo "<h2 class='mt-3'>Differences:</h2>";
echo "<pre>";
print_r($differences);
echo "</pre>";
}
}
function compareJson($json1, $json2, $key) {
$differences = [];
// Compare based on specified key
foreach ($json1["data"][$key] as $item1) {
$id = $item1["id"];
$matchingItem2 = findItemById($json2, $key, $id);
if ($matchingItem2 === null) {
$differences[] = "$key with id $id not found in JSON 2";
}
}
// Compare "total_items" in pagination
if ($json1["data"]["pagination"]["total_items"] !== $json2["data"]["pagination"]["total_items"]) {
$differences[] = "Total items difference in pagination";
}
return $differences;
}
function findItemById($json, $key, $id) {
foreach ($json["data"][$key] as $item) {
if ($item["id"] === $id) {
return $item;
}
}
return null;
}
?>
<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/@popperjs/core@2.5.1/dist/umd/popper.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js"></script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment