---
title: "JSON & API responses — Trading Analytics for Laravel | Roundly"
description: "Return the engine or any single result straight from a controller — every object is Arrayable, Jsonable and JsonSerializable."
url: https://roundly-consulting.com/open-source/docs/trading-analytics-for-laravel/json-output
language: en
---

[All packages](https://roundly-consulting.com/open-source.md)

[Trading Analytics for Laravel](https://roundly-consulting.com/open-source/docs/trading-analytics-for-laravel.md)

# JSON & API responses

The Analytics instance and every result object implement Laravel’s Arrayable and Jsonable contracts plus PHP’s JsonSerializable, so they drop straight into API responses:

```php
// In a controller — returns the full result matrix as JSON
return $analytics;

// Or explicitly
return response()->json($analytics);

$analytics->toArray();   // nested array
$analytics->toJson();    // JSON string
json_encode($analytics); // same payload via JsonSerializable

$analytics->expectancy->toJson(); // any single result serializes the same way
```

Each individual result (e.g. $analytics->expectancy, $analytics->counts) and the NumericValueAsString value object serialize the same way.

## Payload shape

toArray() contains only the metrics that ran, under snake\_case keys; P&L sits under profit\_and\_loss.realized and profit\_and\_loss.unrealized. Amounts and ratios are strings, so no precision is lost on the way to JSON:

```php
array_keys($analytics->toArray());
// ['counts', 'wins', 'volume', 'value', 'commission', 'profit_and_loss',
//  'profit_factor', 'cumulative_return', 'frequency', 'duration', 'streaks',
//  'expectancy', 'risk_reward_ratio', 'win_rate_by_period', 'max_drawdown',
//  'risk_adjusted_returns']

$analytics->toArray()['profit_and_loss']['realized']['net']['pnl']['global']['total'];
// [
//     'total' => '781.00',
//     'average' => '260.33',
//     'highest' => ['value' => '990.00', 'pair' => 'BTC/USD'],
//     'lowest' => ['value' => '-306.00', 'pair' => 'ETH/USD'],
// ]

$analytics->counts->toArray()['per_pair'];
// ['BTC/USD' => ['total' => '1', 'buy' => '1', 'sell' => '0'],
//  'ETH/USD' => ['total' => '2', 'buy' => '1', 'sell' => '1']]
```

- In the per\_pair, per\_base\_currency and per\_quote\_currency breakdowns, count-like results (counts, wins, streaks) leave out entries whose total is zero, while aggregates (volume, value, commission, duration, P&L, cumulative return) list every key a trade fed — a pair whose P&L nets to exactly 0 included — and leave out keys merely read through forPair() and friends.
- wins.win\_ratio lists every key with a closed trade, a 0.00 ratio included.
- frequency serializes each value as a full array — value, scale, prefix, suffix and formatted — so the unit travels with the number.
- Formatted strings include a prefix or suffix when one is set.

## Show your open-source love

This package is free and MIT-licensed. If it saves you time, a one-off donation or a Patreon membership keeps it maintained, tested and documented.

[More ways to support, including crypto](https://roundly-consulting.com/support-us.md)

By donating, you agree to our [donation terms](https://roundly-consulting.com/donation-terms.md).

[Support our open source work (opens in a new tab)](https://donate.stripe.com/dRmeVe8FX5PF1Qd9pXcEw00) [Join us on Patreon (opens in a new tab)](https://www.patreon.com/cw/roundly)

## Want this built into your product?

We integrate our packages into custom Laravel and AI builds. Tell us what you're working on and we'll reply within 48 hours.

[Get a quote in 48 hours](https://roundly-consulting.com/contact.md) [Browse all packages](https://roundly-consulting.com/open-source/docs/trading-analytics-for-laravel.md)
