PHPackages                             focus/data - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. focus/data

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

focus/data
==========

A collection of tools for working with unstructured data, such as JSON.

3.1.0(1y ago)66211MITPHPPHP ^8.2

Since Nov 3Pushed 1y ago1 watchersCompare

[ Source](https://github.com/focusphp/data)[ Packagist](https://packagist.org/packages/focus/data)[ RSS](/packages/focus-data/feed)WikiDiscussions main Synced 1mo ago

READMEChangelogDependencies (6)Versions (9)Used By (1)

Focus: Data
===========

[](#focus-data)

[![Minimum PHP Version](https://camo.githubusercontent.com/096d54c993c13914eb318211d51dc5c6a0320bf10027d3ed00a1ceb7804dd6ca/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f7068702d253345253344253230382e322d3838393242462e7376673f7374796c653d666c6174)](https://php.net/)[![Latest Stable Version](https://camo.githubusercontent.com/b1362a57b3201f1fb207ba5a1cf7851ec49bf2f396fde96c51018fdcd6a337c4/687474703a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f666f6375732f646174612e7376673f7374796c653d666c6174)](https://packagist.org/packages/focus/data)[![CI Status](https://github.com/focusphp/data/actions/workflows/ci.yml/badge.svg?branch=main&event=push)](https://github.com/focusphp/data/actions)[![Code Coverage](https://camo.githubusercontent.com/2a26f4a4b0c7de22df42ecb1211b573813e66c2612e01289ca5e6e597827f7e4/68747470733a2f2f636f6465636f762e696f2f67682f666f6375737068702f646174612f67726170682f62616467652e7376673f746f6b656e3d58464d5257413730464e)](https://codecov.io/gh/focusphp/data)

A collection of tools for working with unstructured data, such as JSON.

Installation
------------

[](#installation)

The best way to install and use this package is with [composer](https://getcomposer.org/):

```
composer require focus/data
```

Usage
-----

[](#usage)

The most basic usage is `KeyedDataObject`, which wraps objects and `KeyedDataArray`, which warps arrays:

```
use Focus\Data\KeyedData\KeyedDataFactory;

$value = [
    'user' => [
        'name' => 'Susan Smith',
        'email' => 'susan@example.com',
        'hobbies' => [
            'football',
            'swimming',
            'reading',
        ],
        'deactivated_at' => null,
    ],
];

// Will create either KeyedDataObject or KeyedDataArray, depending on the value
$data = KeyedDataFactory::from($value);
```

Once you have an instance of data, you can access the values by using dot paths:

```
$name = $data->get(path: 'user.name'); // Susan Smith
$email = $data->get(path: 'user.email'); // susan@example.com
```

Values that do not exist will be returned as `null`:

```
$phone = $data->get(path: 'user.phone'); // null
```

[JMESPath](https://jmespath.org) expressions are also supported using the search() method:

```
$sports = $data->search(path: "user.hobbies[? contains(@, 'ball')]"); // ['football']
```

It is also possible to check for the existence of a path, even when the value is `null`:

```
$deactivated = $data->has(path: 'user.deactivated_at'); // true
```

### JSON Data

[](#json-data)

The `JsonData` object is a proxy with factory methods to create data instances from JSON strings as well as PSR-7 `RequestInterface`, `ServerRequestInterface`, and `ResponseInterface` objects:

```
use Focus\Data\JsonData;

/** @var Psr\Http\Message\ServerRequestInterface $request */
$request = $app->request();

$data = JsonData::fromRequest($request);
```

There are three factory methods for `JsonData`:

- `fromString()` creates data from JSON strings
- `fromRequest()` creates data from PSR-7 (server) requests
- `fromResponse()` creates data from PSR-7 responses

Be aware when calling `JsonData::fromRequest()` with a `ServerRequestInterface` object, the value of `getParsedBody()` will be used by default. To disable this behavior, use:

```
$data = JsonData::fromRequest($request, useParsedBody: false);
```

When using `getParsedBody()` remember that most `ServerRequestInterface` objects will decode the request body with `associative: true`, producing an array. If you wish to have `JsonData->value` be an object, instead of an array, configure your `ServerRequestInterface` to decode request bodies with `associative: false` (default for `json_decode()`)

FAQ
---

[](#faq)

These are some of the most common questions about usage and design of this package.

### Why does has() return true for null values?

[](#why-does-has-return-true-for-null-values)

This allows detecting when input has a value that should not be overwritten. For instance, if an application sets a `deactivated_at` timestamp to indicate that the user has left, it might also need to be able to reactivate the user by setting `deactivated_at: null`:

```
if ($data->get(path: 'user.deactivated_at')) {
    $this->userRepository->deactivate(
        id: $data->get(path: 'user.id'),
        timestamp: $data->get(path: 'user.deactivated_at'),
    );
} elseif ($data->has(path: 'user.deactivated_at')) {
    $this->userRepository->activate(
        id: $data->get(path: 'user.id'),
    );
}
```

If has() did not return true for null values, detecting the existence of a null value would be impossible, since get() returns null for undefined paths.

### Why is there a Data interface?

[](#why-is-there-a-data-interface)

Keen observers will note that `KeyedData` implements a `Data` interface and the existence of the `DataProxy` abstract class. This allows for customization of the implementation, despite `KeyedData` being a `final readonly` class, by using a [proxy object](https://refactoring.guru/design-patterns/proxy) to satisfy the [Open/Closed Principle](https://en.wikipedia.org/wiki/Open%E2%80%93closed_principle).

By default, the `DataProxy` object will forward all calls directly to the source `Data` object. This allows customizing the behavior of any method without having to implement the full `Data`interface. For example, this would modify the get() method to treat `false` values as `null`:

```
use Focus\Data\Data;
use Focus\Data\DataProxy;

final class MyData extends DataProxy
{
    public function get(string $path): mixed
    {
        $value = $this->source()->get($path);

        if ($value === false) {
           return null;
        }

        return $value;
    }
}
```

###  Health Score

33

—

LowBetter than 75% of packages

Maintenance34

Infrequent updates — may be unmaintained

Popularity15

Limited adoption so far

Community12

Small or concentrated contributor base

Maturity60

Established project with proven stability

 Bus Factor1

Top contributor holds 86.2% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~49 days

Recently: every ~72 days

Total

7

Last Release

629d ago

Major Versions

1.2.0 → 2.0.02023-11-15

2.1.0 → 3.0.02024-07-30

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/38203?v=4)[Woody Gilk](/maintainers/shadowhand)[@shadowhand](https://github.com/shadowhand)

---

Top Contributors

[![shadowhand](https://avatars.githubusercontent.com/u/38203?v=4)](https://github.com/shadowhand "shadowhand (25 commits)")[![DrechslerDerek](https://avatars.githubusercontent.com/u/66096364?v=4)](https://github.com/DrechslerDerek "DrechslerDerek (4 commits)")

---

Tags

arraydatadotjsonnotationobjectpath

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/focus-data/health.svg)

```
[![Health](https://phpackages.com/badges/focus-data/health.svg)](https://phpackages.com/packages/focus-data)
```

###  Alternatives

[google/cloud-core

Google Cloud PHP shared dependency, providing functionality useful to all components.

343121.4M79](/packages/google-cloud-core)[aimeos/aimeos-base

Aimeos base layer for abstracting from host environments

2.1k134.0k1](/packages/aimeos-aimeos-base)[phpgt/dom

Modern DOM API.

12412.2M18](/packages/phpgt-dom)[anthropic-ai/sdk

Anthropic PHP SDK

129134.7k5](/packages/anthropic-ai-sdk)[jaxon-php/jaxon-core

Jaxon is an open source PHP library for easily creating Ajax web applications

73142.3k25](/packages/jaxon-php-jaxon-core)[aedart/athenaeum

Athenaeum is a mono repository; a collection of various PHP packages

245.2k](/packages/aedart-athenaeum)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
