PHPackages                             dcarbone/json-writer-plus - 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. dcarbone/json-writer-plus

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

dcarbone/json-writer-plus
=========================

Simple JSON Writer library for PHP 5.3+

0.3.0(8y ago)31.2k2MPL-2.0PHPPHP &gt;=5.4

Since Jun 22Pushed 8y ago2 watchersCompare

[ Source](https://github.com/dcarbone/json-writer-plus)[ Packagist](https://packagist.org/packages/dcarbone/json-writer-plus)[ Docs](https://github.com/dcarbone/json-writer-plus)[ RSS](/packages/dcarbone-json-writer-plus/feed)WikiDiscussions master Synced 6d ago

READMEChangelog (2)DependenciesVersions (5)Used By (0)

json-writer-plus
================

[](#json-writer-plus)

Simple JSON Writer library for PHP 5.3+

The goal for this library was to provide a way to construct a JSON object similar in syntax to the PHP [XMLWriter](http://www.php.net//manual/en/book.xmlwriter.php)class.

Inclusion in your Composer app
------------------------------

[](#inclusion-in-your-composer-app)

Add

```
"dcarbone/json-writer-plus" : "0.2.*"
```

To your application's `composer.json` file.

Learn more about Composer here: [](https://getcomposer.org/)

Basic usage
-----------

[](#basic-usage)

To get started creating your own JSON object:

```
use \DCarbone\JsonWriterPlus;

// Create instance
$jsonWriter = new JsonWriterPlus();

// Start the writer object
$jsonWriter->startJson();

// Open a new object for population
$jsonWriter->writeStartObject();

// Directly write a property name and value to the opened object
$jsonWriter->writeObjectProperty('PropertyKey', 'PropertyValue');

// Write a new property name for later population
$jsonWriter->writeObjectPropertyName('ValueArray');

// Start an array.  Since we wrote a new property name above, it is automatically appended
// to the parent object at the previously specified key
$jsonWriter->writeStartArray();

// Add two values to the array
$jsonWriter->writeValue("Value1");
$jsonWriter->writeValue("Value2");

// Close the array
$jsonWriter->writeEndArray();

// Close the parent object
$jsonWriter->writeEndObject();

// Close the writer
$jsonWriter->endJson();

// See the "jsonized" version of the above actions
echo $jsonWriter->getEncoded()."\n";

// See the internal representation of the above actions as PHP sees it
echo '';
var_dump($jsonWriter->getUnencoded());
echo '';
```

The above code block will result in the following being output:

```
{"PropertyKey":"PropertyValue","ValueArray":["Value1","Value2"]}

object(stdClass)#4 (2) {
  ["PropertyKey"]=>
  string(13) "PropertyValue"
  ["ValueArray"]=>
  array(2) {
    [0]=>
    string(6) "Value1"
    [1]=>
    string(6) "Value2"
  }
}
```

Starting out
------------

[](#starting-out)

The above example demonstrates a Json output with the primary element being an Object, but you may also start things out with an array:

```
// Initialize writer
$jsonWriter = new JsonWriterPlus();

// Start writer
$jsonWriter->startJson();

// Open root array
$jsonWriter->writeStartArray();

// Open object as first item of root array
$jsonWriter->writeStartObject();
$jsonWriter->writeObjectProperty('Property1', 'This object is inside an array!');
$jsonWriter->writeEndObject();

// Open new array as 2nd item of root array
$jsonWriter->writeStartArray();
$jsonWriter->writeValue('Nested array value 1');
$jsonWriter->writeValue('Nested array value 2');
$jsonWriter->writeEndArray();

// Write a string value directly to root array as 3rd item
$jsonWriter->writeValue('Root array value');

$jsonWriter->writeEndArray();

$jsonWriter->endJson();

echo $jsonWriter->getEncoded()."\n";
echo '';
var_dump($jsonWriter->getUnencoded());
echo '';
```

The above will output:

```
[{"Property1":"This object is inside an array!"},["Nested array value 1","Nested array value 2"],"Root array value"]

array(3) {
  [0]=>
  object(stdClass)#4 (1) {
    ["Property1"]=>
    string(31) "This object is inside an array!"
  }
  [1]=>
  array(2) {
    [0]=>
    string(20) "Nested array value 1"
    [1]=>
    string(20) "Nested array value 2"
  }
  [2]=>
  string(16) "Root array value"
}
```

Fun stuff
---------

[](#fun-stuff)

Lets say you have a JsonWriter instance already open and an array already constructed, and you wish to just append the entire thing to the Json output without looping through and manually performing actions. Well, good sir/ma'am/fish, you can!

```
$array = array(
    'Look at all my cool information',
    'My information is the coolest'
);

$jsonWriter = new JsonWriterPlus();

$jsonWriter->startJson();

$jsonWriter->appendArray($array);

$jsonWriter->endJson();

echo $jsonWriter->getEncoded()."\n";
echo '';
var_dump($jsonWriter->getUnencoded());
echo '';
```

The above will output:

```
["Look at all my cool information","My information is the coolest"]
```

```
array(2) {
  [0]=>
  string(31) "Look at all my cool information"
  [1]=>
  string(29) "My information is the coolest"
}
```

```
$array = array(
    'Look at all my cool information',
    'property1' => 'property 1 is the coolest property',
    'property2' => 'property2 is the next coolest property',
    'this is also cool information'
);

$jsonWriter = new JsonWriterPlus();

$jsonWriter->startJson();

$jsonWriter->appendArray($array);

$jsonWriter->endJson();

echo $jsonWriter->getEncoded()."\n";
echo '';
var_dump($jsonWriter->getUnencoded());
echo '';
```

Will result in:

```
["Look at all my cool information",{"property1":"property 1 is the coolest property"},{"property2":"property2 is the next coolest property"},"this is also cool information"]
```

```
array(4) {
  [0]=>
  string(31) "Look at all my cool information"
  [1]=>
  object(stdClass)#4 (1) {
    ["property1"]=>
    string(34) "property 1 is the coolest property"
  }
  [2]=>
  object(stdClass)#5 (1) {
    ["property2"]=>
    string(38) "property2 is the next coolest property"
  }
  [3]=>
  string(29) "this is also cool information"
}
```

You may also perform similar actions with an object via `appendObject($object)`.

###  Health Score

29

—

LowBetter than 59% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity23

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity51

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% 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 ~439 days

Total

4

Last Release

3029d ago

PHP version history (2 changes)0.1.0PHP &gt;=5.3.3

0.3.0PHP &gt;=5.4

### Community

Maintainers

![](https://www.gravatar.com/avatar/385c0c0eae1b51f1e81ee464ff6bfb3cce32589ac252ca68cc3a8aec2e3ada14?d=identicon)[dcarbone](/maintainers/dcarbone)

---

Top Contributors

[![dcarbone](https://avatars.githubusercontent.com/u/1392439?v=4)](https://github.com/dcarbone "dcarbone (8 commits)")

---

Tags

phpjsonjsonwriter

### Embed Badge

![Health badge](/badges/dcarbone-json-writer-plus/health.svg)

```
[![Health](https://phpackages.com/badges/dcarbone-json-writer-plus/health.svg)](https://phpackages.com/packages/dcarbone-json-writer-plus)
```

###  Alternatives

[adhocore/json-fixer

Fix/repair truncated JSON data

51543.2k2](/packages/adhocore-json-fixer)[sbsaga/toon

🧠 TOON for Laravel — a compact, human-readable, and token-efficient data format for AI prompts &amp; LLM contexts. Perfect for ChatGPT, Gemini, Claude, Mistral, and OpenAI integrations (JSON ⇄ TOON).

6115.6k](/packages/sbsaga-toon)[blancks/fast-jsonpatch-php

Class designed to efficiently handle JSON Patch operations in accordance with the RFC 6902 specification

396.4k](/packages/blancks-fast-jsonpatch-php)[josantonius/json

PHP simple library for managing Json files.

1621.6k10](/packages/josantonius-json)

PHPackages © 2026

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