PHPackages                             laktak/hjson - 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. laktak/hjson

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

laktak/hjson
============

JSON for Humans. A configuration file format with relaxed syntax, fewer mistakes and more comments.

v2.3.1(1y ago)86245.3k↓37.1%19[2 issues](https://github.com/hjson/hjson-php/issues)10MITPHPPHP &gt;=5.6CI passing

Since Mar 15Pushed 1y ago6 watchersCompare

[ Source](https://github.com/hjson/hjson-php)[ Packagist](https://packagist.org/packages/laktak/hjson)[ Docs](https://github.com/hjson/hjson-php)[ RSS](/packages/laktak-hjson/feed)WikiDiscussions master Synced yesterday

READMEChangelog (10)Dependencies (1)Versions (14)Used By (10)Security (1)

hjson-php
=========

[](#hjson-php)

[![Build Status](https://github.com/hjson/hjson-php/workflows/test/badge.svg)](https://github.com/hjson/hjson-php/actions)[![Packagist](https://camo.githubusercontent.com/5482a5fd4aabebff9dd6a8e268b89375703831b9d0bd51ff652c9a7f3314adfa/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6c616b74616b2f686a736f6e2e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/laktak/hjson)

[Hjson](https://hjson.github.io), the Human JSON. A configuration file format for humans. Relaxed syntax, fewer mistakes, more comments.

[![Hjson Intro](https://camo.githubusercontent.com/965b434b17f3221d2361904508a9266196e02bc67b4eee44dd60c8a16225239b/68747470733a2f2f686a736f6e2e6769746875622e696f2f686a736f6e312e676966)](https://camo.githubusercontent.com/965b434b17f3221d2361904508a9266196e02bc67b4eee44dd60c8a16225239b/68747470733a2f2f686a736f6e2e6769746875622e696f2f686a736f6e312e676966)

```
{
  # specify rate in requests/second (because comments are helpful!)
  rate: 1000

  // prefer c-style comments?
  /* feeling old fashioned? */

  # did you notice that rate doesn't need quotes?
  hey: look ma, no quotes for strings either!

  # best of all
  notice: []
  anything: ?

  # yes, commas are optional!
}

```

The PHP implementation of Hjson is based on [hjson-js](https://github.com/hjson/hjson-js). For other platforms see [hjson.github.io](https://hjson.github.io).

Install from composer
=====================

[](#install-from-composer)

```
composer require laktak/hjson

```

Usage
=====

[](#usage)

```
use HJSON\HJSONParser;
use HJSON\HJSONStringifier;

$parser = new HJSONParser();
$obj = $parser->parse(hjsonText);

$stringifier = new HJSONStringifier();
$text = $stringifier->stringify($obj);

```

API
===

[](#api)

### HJSONParser: parse($text, $options)

[](#hjsonparser-parsetext-options)

This method parses *JSON* or *Hjson* text to produce an object or array.

- *text*: the string to parse as JSON or Hjson
- *options*: array
    - *keepWsc*: boolean, keep white space and comments. This is useful if you want to edit an hjson file and save it while preserving comments (default false)
    - *assoc*: boolean, return associative array instead of object (default false)

### HJSONStringifier: stringify($value, $options)

[](#hjsonstringifier-stringifyvalue-options)

This method produces Hjson text from a value.

- *value*: any value, usually an object or array.
- *options*: array
    - *keepWsc*: boolean, keep white space. See parse.
    - *bracesSameLine*: boolean, makes braces appear on the same line as the key name. Default false.
    - *quotes*: string, controls how strings are displayed.
        - "min": no quotes whenever possible (default)
        - "always": always use quotes
    - *space*: specifies the indentation of nested structures. If it is a number, it will specify the number of spaces to indent at each level. If it is a string (such as '\\t' or ' '), it contains the characters used to indent at each level.
    - *eol*: specifies the EOL sequence

modify &amp; keep comments
--------------------------

[](#modify--keep-comments)

You can modify a Hjson file and keep the whitespace &amp; comments intact. This is useful if an app updates its config file.

```
$parser = new HJSONParser();
$stringifier = new HJSONStringifier();

$text = "{
  # specify rate in requests/second (because comments are helpful!)
  rate: 1000

  // prefer c-style comments?
  /* feeling old fashioned? */

  # did you notice that rate doesn't need quotes?
  hey: look ma, no quotes for strings either!

  # best of all
  notice: []
  anything: ?

  # yes, commas are optional!

  array: [
    // hello
    0
    1
    2
  ]
}";

// Parse, keep whitespace and comments
$data = $parser->parseWsc($text);

// Modify like you normally would
$data->rate = 500;

// You can also edit comments by accessing __WSC__
$wsc1 = &$data->__WSC__; // for objects
$wsc2 = &$data->array['__WSC__']; // for arrays

// __WSC__ for objects contains { c: {}, o: [] }
// - c with the actual comment and, firts comment is key ' '
// - o (array) with the order of the members
$emptyKey = " ";
$wsc1->c->$emptyKey = "\n  # This is the first comment";
$wsc1->c->rate = "\n  # This is the comment after rate";

// Sort comments order just because we can
sort($wsc1->o);

// Edit array comments
$wsc2[0] .= ' world';

// convert back to Hjson
$text2 = $stringifier->stringifyWsc($data);

```

History
=======

[](#history)

[see releases](https://github.com/hjson/hjson-php/releases)

###  Health Score

50

—

FairBetter than 95% of packages

Maintenance44

Moderate activity, may be stable

Popularity49

Moderate usage in the ecosystem

Community32

Small or concentrated contributor base

Maturity65

Established project with proven stability

 Bus Factor2

2 contributors hold 50%+ of commits

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 ~303 days

Total

12

Last Release

428d ago

Major Versions

v1.0.2 → v2.0.02016-08-08

### Community

Maintainers

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

---

Top Contributors

[![dqsully](https://avatars.githubusercontent.com/u/9079721?v=4)](https://github.com/dqsully "dqsully (17 commits)")[![laktak](https://avatars.githubusercontent.com/u/959858?v=4)](https://github.com/laktak "laktak (16 commits)")[![trobro](https://avatars.githubusercontent.com/u/5781786?v=4)](https://github.com/trobro "trobro (10 commits)")[![peter279k](https://avatars.githubusercontent.com/u/9021747?v=4)](https://github.com/peter279k "peter279k (5 commits)")[![iainhallam](https://avatars.githubusercontent.com/u/282030?v=4)](https://github.com/iainhallam "iainhallam (4 commits)")[![chrisputnam9](https://avatars.githubusercontent.com/u/5308396?v=4)](https://github.com/chrisputnam9 "chrisputnam9 (3 commits)")[![jawb](https://avatars.githubusercontent.com/u/1163113?v=4)](https://github.com/jawb "jawb (3 commits)")[![sprak3000](https://avatars.githubusercontent.com/u/5545555?v=4)](https://github.com/sprak3000 "sprak3000 (2 commits)")[![nichtich](https://avatars.githubusercontent.com/u/299856?v=4)](https://github.com/nichtich "nichtich (1 commits)")[![s-patompong](https://avatars.githubusercontent.com/u/3887531?v=4)](https://github.com/s-patompong "s-patompong (1 commits)")[![andvaranaut](https://avatars.githubusercontent.com/u/12812261?v=4)](https://github.com/andvaranaut "andvaranaut (1 commits)")

---

Tags

hjsonphpjsonconfigparserserializercommentshumanhjson

###  Code Quality

Code StylePHP\_CodeSniffer

### Embed Badge

![Health badge](/badges/laktak-hjson/health.svg)

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

###  Alternatives

[masterminds/html5

An HTML5 parser and serializer.

1.8k269.7M321](/packages/masterminds-html5)[hassankhan/config

Lightweight configuration file loader that supports PHP, INI, XML, JSON, and YAML files

1.0k13.8M185](/packages/hassankhan-config)[salsify/json-streaming-parser

A streaming parser for JSON in PHP.

7887.1M21](/packages/salsify-json-streaming-parser)[cerbero/json-parser

Zero-dependencies pull parser to read large JSON from any source in a memory-efficient way.

806588.3k5](/packages/cerbero-json-parser)[pcrov/jsonreader

JSON Pull Parser

1461.3M6](/packages/pcrov-jsonreader)[zumba/json-serializer

Serialize PHP variables, including objects, in JSON format. Support to unserialize it too.

130784.7k16](/packages/zumba-json-serializer)

PHPackages © 2026

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