PHPackages                             danharper/laravel-jsonx - 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. [Parsing &amp; Serialization](/categories/parsing)
4. /
5. danharper/laravel-jsonx

ActiveLibrary[Parsing &amp; Serialization](/categories/parsing)

danharper/laravel-jsonx
=======================

Add XML support to a JSON-speaking Laravel API with a single middleware via JSONx

v0.1.2(10y ago)1857.2k↓50%1[1 PRs](https://github.com/danharper/JSONx-for-Laravel/pulls)MITPHPCI passing

Since Nov 22Pushed 3mo ago1 watchersCompare

[ Source](https://github.com/danharper/JSONx-for-Laravel)[ Packagist](https://packagist.org/packages/danharper/laravel-jsonx)[ RSS](/packages/danharper-laravel-jsonx/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependencies (4)Versions (6)Used By (0)

JSONx for Laravel
=================

[](#jsonx-for-laravel)

[![](https://camo.githubusercontent.com/9baf584f7e89fc7fecdeaac40be157789b11ff5bc833eadb2b4b7ed159cad5f8/68747470733a2f2f6170692e7472617669732d63692e6f72672f64616e6861727065722f4a534f4e782d666f722d4c61726176656c2e737667)](https://travis-ci.org/danharper/JSONx-for-Laravel) [![Latest Stable Version](https://camo.githubusercontent.com/cee6acad7886269a2f6e67874f06625778c508a12e7ce91de4eb6d0a685d5aec/68747470733a2f2f706f7365722e707567782e6f72672f64616e6861727065722f6c61726176656c2d6a736f6e782f762f737461626c65)](https://packagist.org/packages/danharper/laravel-jsonx) [![License](https://camo.githubusercontent.com/eb55204ae398107009622e15a92597a670bc4ef1788f57e21aa624cdc075767b/68747470733a2f2f706f7365722e707567782e6f72672f64616e6861727065722f6c61726176656c2d6a736f6e782f6c6963656e7365)](https://packagist.org/packages/danharper/laravel-jsonx)

Add XML support to your JSON API just by adding this one middleware. All incoming XML requests are converted to JSON. All outgoing JSON responses are converted to XML.

Requests just need to use the `Accept: application/xml` header to receive the response as XML. And if they're sending in XML, they just need to use the `Content-Type: application/xml` header too.

It does this using IBM's standard for representing JSON as XML: [JSONx](https://tools.ietf.org/html/draft-rsalz-jsonx-00).

This library is just a small wrapper around [`danharper/jsonx`](https://github.com/danharper/jsonx). If you're not using Laravel, or want something based around standard PSR-7 messages, check out [`danharper/psr7-jsonx`](https://github.com/danharper/Psr7JSONx).

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

[](#installation)

```
composer require danharper/laravel-jsonx

```

Register the middleware within `$routeMiddleware` in `app/Http/Kernel`:

```
protected $routeMiddleware => [
  // ...
  'jsonx' => \danharper\LaravelJSONx\JSONxMiddleware::class,
];
```

And simply add the `jsonx` middleware to your API routes.

Example
-------

[](#example)

Once the middleware's registered, use it like so:

```
Route::get('foo', ['middleware' => ['jsonx'], 'uses' => function() {
  return [
    'fruits' => ['apple', 'banana', 'pear'],
    'something' => true,
  ];
});
```

Make a JSON request, e.g. using the `Accept: application/json` header and in response you'll get (as Laravel provides by default):

```
{
  "fruits": ["apple", "banana", "pear"],
  "something": true
}
```

But make an XML request using the `Accept: application/xml` header and you'll get back JSONx:

```

    apple
    banana
    pear

  true

```

Additionally, incoming XML data (formatted as JSONx) will be seamlessly converted to JSON.

So for example, these two are equivalent (assuming they're sent with `Content-Type: application/json` and `Content-Type: application/xml` headers respectively):

```
{
  "address": {
    "line1": "9 Long Street",
    "city": "Portsmouth"
  }
}
```

```

    9 Long Street
    Portsmouth

```

And with a handler:

```
Route::post('/', ['middleware' => ['jsonx'], 'uses' => function() {
  return [
    'hello' => request('address.city')
  ];
});
```

When the response is asked for as JSON (default) as `Accept: application/json` or XML as `Accept: application/xml`, the response will be:

```
{
  "hello": "Portsmouth"
}
```

```

	Portsmouth

```

###  Health Score

43

—

FairBetter than 91% of packages

Maintenance53

Moderate activity, may be stable

Popularity37

Limited adoption so far

Community11

Small or concentrated contributor base

Maturity57

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 87.5% 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 ~0 days

Total

3

Last Release

3830d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/f85b25e3772649f8b096057162897f1052385f956e170e9cdf628c4ef72edede?d=identicon)[danharper](/maintainers/danharper)

---

Top Contributors

[![danharper](https://avatars.githubusercontent.com/u/510740?v=4)](https://github.com/danharper "danharper (14 commits)")[![dependabot[bot]](https://avatars.githubusercontent.com/in/29110?v=4)](https://github.com/dependabot[bot] "dependabot[bot] (1 commits)")[![iWader](https://avatars.githubusercontent.com/u/2007548?v=4)](https://github.com/iWader "iWader (1 commits)")

---

Tags

jsonlaravelxmllumenibmjsonx

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/danharper-laravel-jsonx/health.svg)

```
[![Health](https://phpackages.com/badges/danharper-laravel-jsonx/health.svg)](https://phpackages.com/packages/danharper-laravel-jsonx)
```

###  Alternatives

[bmatovu/laravel-xml

Laravel XML Support

91270.4k](/packages/bmatovu-laravel-xml)[tucker-eric/laravel-xml-middleware

A Laravel Middleware to accept XML requests

181.2M](/packages/tucker-eric-laravel-xml-middleware)[ernysans/laraworld

Countries, Languages and Time Zones Package for Laravel 5.\*

1755.7k](/packages/ernysans-laraworld)

PHPackages © 2026

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