<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Shafiul Azim</title>
    <description>The latest articles on Forem by Shafiul Azim (@shafiulazim).</description>
    <link>https://forem.com/shafiulazim</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1252247%2Fd03c80e0-d2e1-46aa-85d6-9f1c676309da.png</url>
      <title>Forem: Shafiul Azim</title>
      <link>https://forem.com/shafiulazim</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/shafiulazim"/>
    <language>en</language>
    <item>
      <title>Converting Excel File to JSON in JS /React.js /Next.js from an URL.</title>
      <dc:creator>Shafiul Azim</dc:creator>
      <pubDate>Tue, 09 Jan 2024 07:47:18 +0000</pubDate>
      <link>https://forem.com/shafiulazim/converting-excel-file-to-json-in-js-reactjs-nextjs-from-an-url-3gdl</link>
      <guid>https://forem.com/shafiulazim/converting-excel-file-to-json-in-js-reactjs-nextjs-from-an-url-3gdl</guid>
      <description>&lt;p&gt;In web development, dealing with data in various formats is a common task. One frequent scenario is the need to convert Excel files into a more flexible and accessible format, such as JSON. In this blog post, we’ll explore how to achieve this using JavaScript, with a focus on React.js and Next.js.You can use this code in Vanila.js project also.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Getting Started&lt;/strong&gt;&lt;br&gt;
To get started, we’ll use the excellent xlsx library, which provides functionality for reading and writing Excel files.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Installing package&lt;/strong&gt;&lt;br&gt;
Open your code editior &amp;amp; open project folder.Now open terminal on this folder &amp;amp; write this script to install xlsx package.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;npm install xlsx&lt;/code&gt;&lt;br&gt;
or &lt;br&gt;
&lt;code&gt;yarn add xlsx&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;The code we’ll be working with is encapsulated in a module named excelToJson.js.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// excelToJson.js

import * as XLSX from "xlsx";
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This module contains two essential functions: downloadExcelAsArrayBuffer and convertExcelToJson. Let's break down each step of the process.&lt;/p&gt;

&lt;p&gt;Downloading Excel File&lt;br&gt;
The first step is to download the Excel file as an ArrayBuffer. This function uses the fetch API to retrieve the file from a given URL.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// excelToJson.js

import * as XLSX from "xlsx";
//The first step is to download the Excel file as an ArrayBuffer. This function uses the fetch API to retrieve the file from a given URL. 

const downloadExcelAsArrayBuffer = async (excelUrl) =&amp;gt; {
  try {
    // Fetch the Excel file
    const response = await fetch(excelUrl);

    if (!response.ok) {
      throw new Error(`Failed to download Excel file. Status: ${response.status}`);
    }
    // Convert the response to ArrayBuffer
    const arrayBuffer = await response.arrayBuffer();
    return arrayBuffer;
  } catch (error) {
    console.error("Error downloading Excel file:", error);
    throw error;
  }
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Converting Excel to JSON&lt;/strong&gt;&lt;br&gt;
The core functionality lies in the convertExcelToJson function. It performs the conversion from Excel to JSON using the xlsx library.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const convertExcelToJson = async (excelUrl) =&amp;gt; {

  try {
    // Download the Excel file as ArrayBuffer
    const arrayBuffer = await downloadExcelAsArrayBuffer(excelUrl);

    // Convert the Excel buffer to a workbook
    const workbook = XLSX.read(arrayBuffer, { type: "array" });

    // Get the first sheet
    const sheetName = workbook.SheetNames[0];
    const sheet = workbook.Sheets[sheetName];

    // Convert the sheet data to JSON
    const jsonData = XLSX.utils.sheet_to_json(sheet, { header: 1 });

    // Map the array to an array of objects
    const resultArray = jsonData.map((row) =&amp;gt; {
      const obj = {};
      for (let i = 0; i &amp;lt; jsonData[0].length; i++) {
        obj[jsonData[0][i]] = row[i];
      }
      return obj;
    });

    return resultArray.slice(1);
  } catch (error) {
    console.error("Error converting Excel to JSON:", error);
    throw error;
  }
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Implementing in React.js/Next.js&lt;/strong&gt;&lt;br&gt;
To use this functionality in a React.js or Next.js project, simply import the convertExcelToJson function and use it as needed.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import convertExcelToJson from "./path/to/excelToJson";

const YourComponent = async () =&amp;gt; {
  try {
    const jsonData = await convertExcelToJson("your_excel_file_url.xlsx");
    console.log(jsonData);
    // Now you can use jsonData in your React component
  } catch (error) {
    // Handle errors
  }
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;With this script, you can easily convert downloadable Excel files to JSON in your JavaScript projects. This process is especially handy when dealing with data imports and exports in web applications.&lt;/p&gt;

&lt;p&gt;Happy coding!&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>react</category>
      <category>tutorial</category>
      <category>webdev</category>
    </item>
    <item>
      <title>External or remote image size compression / optimization in Next.js</title>
      <dc:creator>Shafiul Azim</dc:creator>
      <pubDate>Tue, 09 Jan 2024 07:37:37 +0000</pubDate>
      <link>https://forem.com/shafiulazim/external-or-remote-image-size-compression-optimization-in-nextjs-3afc</link>
      <guid>https://forem.com/shafiulazim/external-or-remote-image-size-compression-optimization-in-nextjs-3afc</guid>
      <description>&lt;p&gt;Sometimes we need to reduce the size of the image that are getting from external API.As it’s just an url not the file &amp;amp; we use custom loader we don’t get the Next js Image optimization feature.Sometimes we had to implement prefix in the url and also prefix before static assests (“example/_next/static/….) we don’t get default next js image optimization feature if we use custom loader in the Image component.&lt;/p&gt;

&lt;p&gt;First create a imageLoader.js file in top level of your project directory and paste the following code,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const imageLoader = ({ src, width, quality }) =&amp;gt; {

  if (!src?.includes("https")) {
    return `${src}?w=${width}&amp;amp;q=${quality || 75}`; //for static images
  } else {
    src = src.split("https://").join(""); //will remove "https://" from the url to avoid CORS error
  }

  let secretUrl = encodeURIComponent(src); //encode image source url.

  const apiEndpoint = "/api/imageloader" //Write your internal(next js) api here

  return `${
    window?.location?.origin
  }${apiEndpoint}?url=${secretUrl}&amp;amp;w=${width}&amp;amp;q=${quality || 75}`;
};

export default imageLoader;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now create an api for image optimization under api folder “api/imageloader.js” and paste the following code,we use redis for caching and reduce server pressure.If you want you can ignore redis.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import axios from "axios";
import sharp from "sharp";
import redis from "../../../lib/redis";


export default async function imageLoader(req, res) {
  let url = decodeURIComponent(req?.query?.url); //decoding the url
  url = `https://${url}`; //adding https:// that we were removed in the previos file

  try {

    if (!url) {
      return res.status(400).send("400 Bad request. url is missing");
    }

    const cachedUrl = await redis.getBuffer(`${url}`); //if already cached ther return cached one &amp;amp; reduce server load

    if (!cachedUrl) {
      const width = req.query.w ?? "384"; //default width
      const quality = req.query.q ?? "86"; //default quality
      // get the image information
      const response = await axios.get(url, {
        responseType: "arraybuffer",
      });

      // use sharp lib to resize the image based on the parameters
      const optimized = await sharp(response.data)
        .resize({
          withoutEnlargement: true,
          width: parseInt(width),
        })
        .webp({ quality: parseInt(quality) }) //transform image to webp format
        .toBuffer();

      // set public cache to 10  min
      res.setHeader("Cache-Control", "public, max-age=600, must-revalidate");

      // set content type to webp.
      res.setHeader("content-type", "image/webp");

      await redis.set(`${url}`, optimized);

      // send buffered image
      return res.status(200).send(optimized);
    }

    res.setHeader("content-type", "image/webp");
    return res.status(200).send(cachedUrl);

  } catch (e) {
    return res.status(e?.response?.status || 401).json({ message: e.message });
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now we are ready to use our optimization api and loader.Pass the loader to the Image component.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;Image
    loader={imageLoader}
    src={imageUrl}
    alt="external image"
   /&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We are good to go.Now enjoy your custom image optimization for next js external image.&lt;/p&gt;

&lt;p&gt;Please feel free to share if this article helps you.&lt;/p&gt;

</description>
      <category>nextjs</category>
      <category>nextjsimage</category>
      <category>imagecompression</category>
      <category>imageprocessing</category>
    </item>
  </channel>
</rss>
