Open PortfolioOpen Portfolio.
โ† Back to Blog

The Guide to API Response Transformation

July 6, 2026at 2:01 PM UTCBy Pocket Portfolio TeamTech
The Guide to API Response Transformation
#api#response#transformation#javascript

Problem

APIs often return data in a format that may not directly align with the needs of your application. This mismatch can lead to increased complexity in data handling and processing. As a developer, you need a robust solution that streamlines the transformation of API responses to ensure that the data is in the desired structure before it's consumed by your application logic.

Solution with Code

To address this issue, we can utilize JavaScript to transform API responses into the desired format. Below is a concise example demonstrating how to achieve this using JavaScript's map and reduce functions.

Example Scenario

Suppose we are working with an API that returns a list of users with nested address objects, but for our application, we need a flattened list of users with a single address string.

Initial API Response

[
  {
    "id": 1,
    "name": "John Doe",
    "address": {
      "street": "123 Elm St",
      "city": "Springfield",
      "state": "IL"
    }
  },
  {
    "id": 2,
    "name": "Jane Smith",
    "address": {
      "street": "456 Oak St",
      "city": "Shelbyville",
      "state": "IN"
    }
  }
]

Desired Output

[
  {
    "id": 1,
    "name": "John Doe",
    "address": "123 Elm St, Springfield, IL"
  },
  {
    "id": 2,
    "name": "Jane Smith",
    "address": "456 Oak St, Shelbyville, IN"
  }
]

Transformation Code

function transformApiResponse(apiResponse) {
  return apiResponse.map(user => ({
    ...user,
    address: `${user.address.street}, ${user.address.city}, ${user.address.state}`
  }));
}

const apiResponse = [
  {
    id: 1,
    name: "John Doe",
    address: {
      street: "123 Elm St",
      city: "Springfield",
      state: "IL"
    }
  },
  {
    id: 2,
    name: "Jane Smith",
    address: {
      street: "456 Oak St",
      city: "Shelbyville",
      state: "IN"
    }
  }
];

const transformedResponse = transformApiResponse(apiResponse);
console.log(transformedResponse);

Key Concepts

  1. Mapping Over Data: The map function is used to iterate over the array of users and apply a transformation to each element. This is particularly useful for altering the shape or content of each object in an array.

  2. String Interpolation: Template literals in JavaScript (enclosed in backticks) allow for easy insertion of variables into strings, making it straightforward to construct complex strings like concatenated addresses.

  3. Spread Operator: The spread operator (...) is used to copy properties from one object to another. This is useful for maintaining existing data while modifying specific properties.

By applying these techniques, developers can efficiently transform API responses, ensuring the data aligns perfectly with application requirements, which simplifies data handling and improves maintainability.

The Guide to API Response Transformation | Open Portfolio Blog | Open Portfolio