JAVASCRIPT

Transforming and Mapping API Response Data to a UI Model

Learn to effectively transform and map raw API response data into a clean, consistent, and UI-friendly data structure for easier frontend integration and display.

// Assume this is the raw data received from an API
const rawApiProduct = {
  productId: 'P101',
  productName: 'Super Widget Pro',
  productDescription: 'An advanced widget with superior performance.',
  productPrice: {
    amount: 99.99,
    currency: 'USD'
  },
  availableStock: 150,
  manufacturerDetails: {
    name: 'Acme Corp',
    country: 'USA',
    contactEmail: '[email protected]'
  },
  category_id: 'CAT001',
  lastUpdated: '2023-10-27T10:30:00Z',
  // ... other verbose or inconsistent fields
};

// Define a function to transform the raw data into a UI-friendly format
function transformProductForUI(rawProduct) {
  if (!rawProduct) return null;

  return {
    id: rawProduct.productId,
    name: rawProduct.productName,
    description: rawProduct.productDescription,
    price: rawProduct.productPrice ? `${rawProduct.productPrice.amount} ${rawProduct.productPrice.currency}` : 'N/A',
    stock: rawProduct.availableStock,
    manufacturer: rawProduct.manufacturerDetails ? rawProduct.manufacturerDetails.name : 'Unknown',
    lastUpdatedDate: rawProduct.lastUpdated ? new Date(rawProduct.lastUpdated).toLocaleDateString() : 'N/A',
    // Example of a computed property or derived value
    isInStock: rawProduct.availableStock > 0
  };
}

// Example Usage:
// const uiProduct = transformProductForUI(rawApiProduct);
// console.log('Transformed UI Product:', uiProduct);
/*
Expected Output:
{
  id: 'P101',
  name: 'Super Widget Pro',
  description: 'An advanced widget with superior performance.',
  price: '99.99 USD',
  stock: 150,
  manufacturer: 'Acme Corp',
  lastUpdatedDate: '10/27/2023', // Varies by locale
  isInStock: true
}
*/

// Example with an array of products
const rawApiProducts = [
  rawApiProduct,
  {
    productId: 'P102',
    productName: 'Mini Gadget',
    productDescription: 'A compact and portable gadget.',
    productPrice: { amount: 29.99, currency: 'EUR' },
    availableStock: 0,
    manufacturerDetails: { name: 'Gizmo Co.' },
    lastUpdated: '2023-10-25T15:00:00Z',
  }
];

const uiProducts = rawApiProducts.map(transformProductForUI);
// console.log('Transformed UI Products:', uiProducts);
How it works: This snippet illustrates a crucial pattern in web development: transforming raw, often verbose or inconsistently structured, API response data into a clean, predictable, and UI-friendly format. The `transformProductForUI` function takes an object representing an API product and maps its fields to a new object with simplified names, formatted values (e.g., price string, formatted date), and even adds derived properties (`isInStock`). This approach isolates UI logic from API specifics, making the frontend code cleaner, more maintainable, and resilient to changes in the API's internal structure. It's particularly useful when dealing with multiple APIs or complex data models.

Need help integrating this into your project?

Our team of expert developers can help you build your custom application from scratch.

Hire DigitalCodeLabs