DEV Community


Posted on

Library to support JSON with comments and trailing comma in PHP

Unlike XML and YAML, JSON lacks native support for comments which is often missed. In addition, the trailing comma which would make diff on multiline value neat is also not available in JSON. If you are on PHP, then here is a little library adhocore/json-comment to fix it in the userland.

Thanks to this library, a JSON like this will be possible:

  "name": "adhocore/json-comment",
  "description": "JSON comment stripper library for PHP",
  "type":/* This is creepy comment */ "library",
  "keywords": [
    // Single line comment, Notice the comma below:
  "license": "MIT",
   * This is a multiline comment.
  "authors": [
      "name": "Jitendra Adhikari",
      "email": "",
  "autoload": {
      "psr-4": {
          "Ahc\\Json\\": "src/",
  "autoload-dev": {
      "psr-4": {
          "Ahc\\Json\\Test\\": "tests/",
  "require": {
      "php": ">=7.0",
      "ext-ctype": "*",
  "require-dev": {
      "phpunit/phpunit": "^6.5 || ^7.5 || ^8.5",
  "scripts": {
      "test": "phpunit",
      "echo": "echo '// This is not comment'",
      "test:cov": "phpunit --coverage-text",
Enter fullscreen mode Exit fullscreen mode

Installation and Usage

composer req adhocore/json-comment
Enter fullscreen mode Exit fullscreen mode
use Ahc\Json\Comment;

// Only strip and do not decode it already:
(new Comment)->strip('{
  // Hmmm

// Strip comment and decode as JSON:
  // Hmmm
}', true);

// Read JSON file, strip comments and decode as JSON:
Comment::parseFromFile('/path/to/commented.json', true);
Enter fullscreen mode Exit fullscreen mode

PS: A new version 1.1.0 of this library has been just released.

Please use it and let know in the feedback or repo ticket if any issues.

GitHub logo adhocore / php-json-comment

Lightweight JSON comment and trailing comma stripper library for PHP


Latest Version Travis Build Scrutinizer CI Codecov branch StyleCI Software License Donate 15 Donate 25 Donate 50 Tweet

  • Lightweight JSON comment stripper library for PHP.
  • Makes possible to have comment in any form of JSON data.
  • Supported comments: single line // comment or multi line /* comment */.
  • Also strips trailing comma at the end of array or object, eg
    • [1,2,,] => [1,2]
    • {"x":1,,} => {"x":1}
  • Handles literal LF (newline/linefeed) within string notation so that we can have multiline string
  • Supports JSON string inside JSON string (see ticket #15 and PR #16)


composer require adhocore/json-comment
# for php5.6
composer require adhocore/json-comment:^0.2
Enter fullscreen mode Exit fullscreen mode


use Ahc\Json\Comment
// The JSON string!
$someJsonText = '{"a":1,
"b":2,// comment
"c":3 /* inline comment */,
// comment
"d":/* also a comment */"d",
/* creepy comment*/"e":2.3,
/* multi line
comment */

// OR
$someJsonText = file_get_contents('...');

// Strip only!
(new Comment)->strip($someJsonText);

// Strip and
Enter fullscreen mode Exit fullscreen mode

Top comments (2)

loru88 profile image

What's the benefits of using this non standard JSON format over YAML?

adhocore profile image

it has been answered by experts

also fyi: