2017 © Pedro Peláez
 

library jsonlint

JSON Linter

image

seld/jsonlint

JSON Linter

  • Friday, April 13, 2018
  • by Seldaek
  • Repository
  • 20 Watchers
  • 443 Stars
  • 11,027,481 Installations
  • PHP
  • 111 Dependents
  • 3 Suggesters
  • 45 Forks
  • 2 Open issues
  • 17 Versions
  • 9 % Grown

The README.md

JSON Lint

Build Status, (*1)

Usage

use Seld\JsonLint\JsonParser;

$parser = new JsonParser();

// returns null if it's valid json, or a ParsingException object.
$parser->lint($json);

// Call getMessage() on the exception object to get
// a well formatted error message error like this

// Parse error on line 2:
// ... "key": "value"    "numbers": [1, 2, 3]
// ----------------------^
// Expected one of: 'EOF', '}', ':', ',', ']'

// Call getDetails() on the exception to get more info.

// returns parsed json, like json_decode() does, but slower, throws
// exceptions on failure.
$parser->parse($json);

You can also pass additional flags to JsonParser::lint/parse that tweak the functionality:, (*2)

  • JsonParser::DETECT_KEY_CONFLICTS throws an exception on duplicate keys.
  • JsonParser::ALLOW_DUPLICATE_KEYS collects duplicate keys. e.g. if you have two foo keys they will end up as foo and foo.2.
  • JsonParser::PARSE_TO_ASSOC parses to associative arrays instead of stdClass objects.
  • JsonParser::ALLOW_COMMENTS parses while allowing (and ignoring) inline // and multiline /* */ comments in the JSON document.

Example:, (*3)

$parser = new JsonParser;
try {
    $parser->parse(file_get_contents($jsonFile), JsonParser::DETECT_KEY_CONFLICTS);
} catch (DuplicateKeyException $e) {
    $details = $e->getDetails();
    echo 'Key '.$details['key'].' is a duplicate in '.$jsonFile.' at line '.$details['line'];
}

Note: This library is meant to parse JSON while providing good error messages on failure. There is no way it can be as fast as php native json_decode()., (*4)

It is recommended to parse with json_decode, and when it fails parse again with seld/jsonlint to get a proper error message back to the user. See for example how Composer uses this library:, (*5)

Installation

For a quick install with Composer use:, (*6)

composer require seld/jsonlint

JSON Lint can easily be used within another app if you have a PSR-4 autoloader, or it can be installed through Composer for use as a CLI util. Once installed via Composer you can run the following command to lint a json file or URL:, (*7)

$ bin/jsonlint file.json

Requirements

  • PHP 5.3+
  • [optional] PHPUnit 3.5+ to execute the test suite (phpunit --version)

Submitting bugs and feature requests

Bugs and feature request are tracked on GitHub, (*8)

Author

Jordi Boggiano - j.boggiano@seld.be - http://twitter.com/seldaek, (*9)

License

JSON Lint is licensed under the MIT License - see the LICENSE file for details, (*10)

Acknowledgements

This library is a port of the JavaScript jsonlint library., (*11)

The Versions

13/04 2018

dev-master

9999999-dev

JSON Linter

  Sources   Download

MIT

The Requires

  • php ^5.3 || ^7.0

 

The Development Requires

parser json validator linter

24/01 2018

1.7.1

1.7.1.0

JSON Linter

  Sources   Download

MIT

The Requires

  • php ^5.3 || ^7.0

 

The Development Requires

parser json validator linter

03/01 2018

1.7.0

1.7.0.0

JSON Linter

  Sources   Download

MIT

The Requires

  • php ^5.3 || ^7.0

 

The Development Requires

parser json validator linter

30/11 2017

1.6.2

1.6.2.0

JSON Linter

  Sources   Download

MIT

The Requires

  • php ^5.3 || ^7.0

 

The Development Requires

parser json validator linter

18/06 2017

1.6.1

1.6.1.0

JSON Linter

  Sources   Download

MIT

The Requires

  • php ^5.3 || ^7.0

 

The Development Requires

parser json validator linter

06/03 2017

1.6.0

1.6.0.0

JSON Linter

  Sources   Download

MIT

The Requires

  • php ^5.3 || ^7.0

 

The Development Requires

parser json validator linter

14/11 2016

1.5.0

1.5.0.0

JSON Linter

  Sources   Download

MIT

The Requires

  • php ^5.3 || ^7.0

 

parser json validator linter

14/09 2016

1.4.1

1.4.1.0

JSON Linter

  Sources   Download

MIT

The Requires

  • php ^5.3 || ^7.0

 

parser json validator linter

21/11 2015

1.4.0

1.4.0.0

JSON Linter

  Sources   Download

MIT

The Requires

  • php ^5.3 || ^7.0

 

parser json validator linter

04/01 2015

1.3.1

1.3.1.0

JSON Linter

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

parser json validator linter

05/09 2014

1.3.0

1.3.0.0

JSON Linter

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

parser json validator linter

20/07 2014

1.2.0

1.2.0.0

JSON Linter

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

parser json validator linter

04/11 2013

1.1.2

1.1.2.0

JSON Linter

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

parser json validator linter

12/02 2013

1.1.1

1.1.1.0

JSON Linter

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

parser json validator linter

13/12 2012

1.1.0

1.1.0.0

JSON Linter

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

parser json validator linter

13/08 2012

1.0.1

1.0.1.0

JSON Linter

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

parser json validator linter

12/03 2012

1.0.0

1.0.0.0

JSON Linter

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

parser json validator linter