Next.js Discord

Discord Forum

Correct approach to load a JSON file into a class

Unanswered
Bombay posted this in #help-forum
Open in Discord
BombayOP
Hi, I am working on a project right now, and was wondering if this is the correct approach to load a local json file, into a list of objects:

import data from "../../data.json";
import { Product } from "../../app/product";

export default function Headphones() {
  const products = data.map((productData) => {
    return new Product(
      productData.id,
      productData.slug,
      productData.name,
      productData.image,
      productData.category,
      productData.categoryImage,
      productData.new,
      productData.price,
      productData.description,
      productData.features,
      productData.includes,
      productData.gallery,
      productData.others
    );
  });


It works as intended, but I would just like to know if there is a better/more efficient approach?

0 Replies