please let my functions return json instead:

function fetchProductProfile($productId) {
    // Database credentials
    $servername = "localhost";
    $username = "tester";
    $password = "Baby@0987";
    $dbname = "your_database_name";

    // Create connection
    $conn = new mysqli($servername, $username, $password, $dbname);

    // Check connection
    if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
    }

    // Prepare and execute the query for the main product
    $stmt = $conn->prepare("SELECT * FROM products WHERE id = ?");
    $stmt->bind_param("i", $productId);
    $stmt->execute();
    $result = $stmt->get_result();

    // Fetch the product
    if ($result->num_rows > 0) {
        $product = $result->fetch_assoc();

        // Load the template
        $template = file_get_contents('page.html');

        // Replace placeholders with product data
        $template = str_replace('{{name}}', htmlspecialchars($product['name']), $template);
        $template = str_replace('{{id}}', htmlspecialchars($product['id']), $template);
        $template = str_replace('{{price}}', htmlspecialchars($product['price']), $template);
        $template = str_replace('{{description}}', htmlspecialchars($product['description']), $template);
        $images = json_decode($product['images'], true);
        $template = str_replace('{{image}}', htmlspecialchars($images[0] ?? ''), $template);
        $template = str_replace('{{category}}', htmlspecialchars($product['category']), $template);
        $template = str_replace('{{stock}}', htmlspecialchars($product['stock']), $template);

        // Fetch and insert related products
        $relatedProductsHtml = getRelatedProducts($conn, $product['category'], $productId);
        $template = str_replace('<!-- Related products dynamically generated -->', $relatedProductsHtml, $template);

        // Output the filled template
        echo $template;
    } else {
        header("HTTP/1.0 404 Not Found");
        echo "Product not found";
    }

    // Close the statement and connection
    $stmt->close();
    $conn->close();
}

function getRelatedProducts($conn, $category, $currentProductId) {
    // Prepare and execute the query for related products
    $stmt = $conn->prepare("SELECT id, name, price, images FROM products WHERE category = ? AND id != ? LIMIT 4");
    $stmt->bind_param("si", $category, $currentProductId);
    $stmt->execute();
    $result = $stmt->get_result();
    
    $relatedProductsHtml = '';
    while ($row = $result->fetch_assoc()) {
        $images = json_decode($row['images'], true);
        $mainImage = $images[0] ?? '';
        $relatedProductsHtml .= "
            <div class='product-card'>
                <div class='product-image'>
                    <img src='" . htmlspecialchars($mainImage) . "' alt='" . htmlspecialchars($row['name']) . "' class='product-thumbnail'>
                </div>
                <div class='product-info'>
                    <h3 class='product-title'>" . htmlspecialchars($row['name']) . "</h3>
                    <p class='product-price'>" . htmlspecialchars($row['price']) . " Ksh</p>
                    <button class='add-to-cart' data-product-id='" . htmlspecialchars($row['id']) . "'>Add to Cart</button>
                    <a href='/product/" . htmlspecialchars($row['id']) . "' class='view-product-btn'>View Product</a>
                </div>
            </div>
        ";
    }
    
    // If no related products found
    if (empty($relatedProductsHtml)) {
        $relatedProductsHtml = "<p>No related products found.</p>";
    }
    
    return $relatedProductsHtml;
}