Skip to content

Map a JSON response to your PHP object

JsonMapper is a powerful open-source package that maps JSON data to PHP classes with ease. It offers a series of middleware that can be arranged and sorted to meet your specific needs, making it a flexible solution for a wide range of use cases.

composer require json-mapper/json-mapper

Get started Read the docs View on GitHub

A few lines is all it takes

With just a few lines of code you can map a JSON string to a PHP class and start working with it right away.

class User
{
public string $name;
}
 
$mapper = \JsonMapper\JsonMapperBuilder::new()
->withDocBlockAnnotationsMiddleware()
->withTypedPropertiesMiddleware()
->build();
 
$user = $mapper->mapToClassFromString('{ "name": "John Doe" }', User::class);
 
echo $user->name; // "John Doe"

Why use JsonMapper

Strong typing

Support for typed properties makes it easy to map JSON with strong type checking, on scalar values as well as nested objects. On PHP 8.1 and up, enums work out of the box, and custom constructors and readonly properties are supported too.

Maps to your code

JsonMapper resolves namespace uses, so your implementation can stay organised the way you want it. Map properties with PHP attributes or DocBlock annotations, transform values through a callback, convert between naming conventions, and debug the mapping as it happens.

Extensible by design

If the out-of-the-box middleware do not meet your needs, writing your own is straightforward. Arrange the chain to fit your use case, and drop in custom middleware wherever you need it.

Using a framework?

JsonMapper ships first-party integrations, so there is little to wire up yourself.

Laravel Symfony