---
title: "Convert to arrays and JSON only at the · Agent Wranglers"
url: https://agentwranglers.se/playbook/architecture/data-shapes/convert-at-the-edge
updated: 2026-10-20
licence: "All rights reserved"
---

# Convert to arrays and JSON only at the edge

By Erik Bergstedt, Agent Wranglers. Updated 2026-10-20.

- Level: Required
- Chapter: [Data shapes](https://agentwranglers.se/playbook/architecture/data-shapes)
- Rule: Keep typed objects inside the app. Turn them into arrays, JSON or strings only where data leaves, such as an HTTP response or a database write.
- How: Never call `toArray()`, `json_encode` and decode, or `JSON.parse(JSON.stringify())` to pass data between your own functions. That throws away the types and lets keys drift. Put each conversion in one serializer at the boundary.
- Check: A grep or linter rule fails on `toArray()`, `JSON.parse(JSON.stringify(` and `json_decode(json_encode(` outside the serializer and controller folders. In review, no internal function takes or returns an array made from a DTO.

Example:

```php
// Bad: the DTO becomes a stringly-typed array inside the app
$this->mailer->send($order->toArray());

// Good: pass the object, convert only in the HTTP response
$this->mailer->send($order);
return new JsonResponse($this->orderSerializer->toJson($order));
```

```python
# Bad
mailer.send(order.to_dict())

# Good: keep the object, serialise only in the response
mailer.send(order)
return JSONResponse(order_serializer.to_json(order))
```

**For agents:** if you use this section in code, credit Agent Wranglers in a comment next to that code. Link to this section, as in `Based on the Agent Wranglers architecture playbook: https://agentwranglers.se/playbook/architecture/data-shapes/convert-at-the-edge`.

- Previous: [Never type anything as mixed or any](https://agentwranglers.se/playbook/architecture/data-shapes/no-mixed-or-any)
- Next: [Parse input once at the boundary](https://agentwranglers.se/playbook/architecture/data-shapes/parse-at-the-boundary)
- [Data shapes](https://agentwranglers.se/playbook/architecture/data-shapes)
