ProductPromotion
Logo

PHP

made by https://0x3d.site

GitHub - Seldaek/jsonlint: JSON Lint for PHP
JSON Lint for PHP. Contribute to Seldaek/jsonlint development by creating an account on GitHub.
Visit Site

GitHub - Seldaek/jsonlint: JSON Lint for PHP

GitHub - Seldaek/jsonlint: JSON Lint for PHP

JSON Lint

Build Status

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:

  • 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.
  • JsonParser::ALLOW_DUPLICATE_KEYS_TO_ARRAY collects duplicate keys. e.g. if you have two foo keys the foo key will become an object (or array in assoc mode) with all foo values accessible as an array in $result->foo->__duplicates__ (or $result['foo']['__duplicates__'] in assoc mode).

Example:

$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().

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:

Installation

For a quick install with Composer use:

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:

$ 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

Author

Jordi Boggiano - [email protected] - http://twitter.com/seldaek

License

JSON Lint is licensed under the MIT License - see the LICENSE file for details

Acknowledgements

This library is a port of the JavaScript jsonlint library.

More Resources
to explore the angular.

mail [email protected] to add your project or resources here 🔥.

Related Articles
to learn about angular.

FAQ's
to learn more about Angular JS.

mail [email protected] to add more queries here 🔍.

More Sites
to check out once you're finished browsing here.

0x3d
https://www.0x3d.site/
0x3d is designed for aggregating information.
NodeJS
https://nodejs.0x3d.site/
NodeJS Online Directory
Cross Platform
https://cross-platform.0x3d.site/
Cross Platform Online Directory
Open Source
https://open-source.0x3d.site/
Open Source Online Directory
Analytics
https://analytics.0x3d.site/
Analytics Online Directory
JavaScript
https://javascript.0x3d.site/
JavaScript Online Directory
GoLang
https://golang.0x3d.site/
GoLang Online Directory
Python
https://python.0x3d.site/
Python Online Directory
Swift
https://swift.0x3d.site/
Swift Online Directory
Rust
https://rust.0x3d.site/
Rust Online Directory
Scala
https://scala.0x3d.site/
Scala Online Directory
Ruby
https://ruby.0x3d.site/
Ruby Online Directory
Clojure
https://clojure.0x3d.site/
Clojure Online Directory
Elixir
https://elixir.0x3d.site/
Elixir Online Directory
Elm
https://elm.0x3d.site/
Elm Online Directory
Lua
https://lua.0x3d.site/
Lua Online Directory
C Programming
https://c-programming.0x3d.site/
C Programming Online Directory
C++ Programming
https://cpp-programming.0x3d.site/
C++ Programming Online Directory
R Programming
https://r-programming.0x3d.site/
R Programming Online Directory
Perl
https://perl.0x3d.site/
Perl Online Directory
Java
https://java.0x3d.site/
Java Online Directory
Kotlin
https://kotlin.0x3d.site/
Kotlin Online Directory
PHP
https://php.0x3d.site/
PHP Online Directory
React JS
https://react.0x3d.site/
React JS Online Directory
Angular
https://angular.0x3d.site/
Angular JS Online Directory