# Convert Nested JSON API Response to Flat Object

> REST API responses include fields Frontend / Web never renders. Map id, status, and UI columns at the edge after calling https://jsonplaceholder.typicode.com/users/{{query.userId}}.

- Canonical page: https://realcomposer.com/recipes/flatten-nested-json-api-response
- Client platform: Frontend / Web
- Upstream services: REST API
- Category: Payload Slimmer
- JSON version: https://realcomposer.com/recipes/flatten-nested-json-api-response.json
- Install in RealComposer (free plan, pre-fills this pipeline): https://realcomposer.com/dashboard/pipelines/new?recipe=flatten-nested-json-api-response&via=agent

## Problem: Unmapped REST API fields waste bandwidth

REST API returns nested CRM, audit, and configuration objects. Frontend / Web parses them on the main thread even when the UI binds three fields.

- Large arrays kept in memory after navigation
- Analytics showing poor LCP on API-heavy screens
- Developers shipping defensive null checks for unused paths

## Solution steps

1. **Store upstream credentials in Secret Vault**: Add API_BEARER_TOKEN to RealComposer Secret Vault. Values decrypt only inside the Cloudflare Worker that calls the upstream.

2. **Configure upstream steps and output mapping**: Configure headersTemplate to inject vault secrets, target https://jsonplaceholder.typicode.com/users/{{query.userId}}, and Map only the UI fields in output mapping so clients never download audit or nested blobs.

3. **Call from Frontend / Web**: Allowed origins on the token should list every Frontend / Web host that calls this URL.

```
const res = await fetch('https://realcomposer.com/v1/workspace/flatten-nested-json-api-response', {
  headers: { Authorization: 'Bearer rc_live_your_token' }
});
```

## Pipeline config

Import this config into a RealComposer pipeline. `{{secrets.NAME}}` values are read from the encrypted Secret Vault at request time, so the client never sees them.

```json
{
  "name": "Convert Nested JSON API Response to Flat Object",
  "method": "GET",
  "cacheTtlSeconds": 0,
  "authMode": "client_token",
  "steps": [
    {
      "stepKey": "upstream",
      "upstreamUrlTemplate": "https://jsonplaceholder.typicode.com/users/{{query.userId}}",
      "httpMethod": "GET",
      "headersTemplate": {
        "Authorization": "Bearer {{secrets.API_BEARER_TOKEN}}"
      },
      "timeoutMs": 3000
    }
  ],
  "outputMapping": {
    "version": 1,
    "fields": [
      {
        "targetPath": "userId",
        "sourcePath": "id",
        "type": "number",
        "sourceStep": "upstream"
      },
      {
        "targetPath": "name",
        "sourcePath": "name",
        "type": "string",
        "sourceStep": "upstream"
      },
      {
        "targetPath": "email",
        "sourcePath": "email",
        "type": "string",
        "sourceStep": "upstream"
      }
    ]
  }
}
```

## Call the pipeline

```bash
curl -X GET "https://realcomposer.com/v1/workspace/flatten-nested-json-api-response" \
  -H "Authorization: Bearer rc_live_client_token"
```

## Payload before and after

Raw upstream response:

```json
{
  "requestId": "req_flatten-",
  "upstreamLatencyMs": 142,
  "audit": {
    "region": "edge",
    "trace": "flatten-nested-json-api-response"
  },
  "payload": {
    "id": "sample_id",
    "status": "active",
    "nested": {
      "tags": [
        "demo"
      ],
      "internal": true
    },
    "unused_blob": {
      "foo": "bar",
      "bar": "baz"
    }
  }
}
```

Slimmed RealComposer response:

```json
{
  "userId": "sample",
  "name": "sample",
  "email": "sample"
}
```

## FAQ

### Is it safe to log REST API responses in Frontend / Web?

Use scoped tokens where the vendor allows them on flatten-nested-json-api-response. Admin keys stay {{secrets.*}} references only.

### When does edge caching apply to this recipe?

flatten-nested-json-api-response keeps cache TTL at 0 because it uses POST upstream calls or per-user responses. Add ids to query strings before enabling GET cache.

### What happens if an upstream step times out?

Allowed origins on the flatten-nested-json-api-response client token restrict which domains may call the edge URL.
