A powerful, intuitive, and comprehensive JSON management library for PHP 8+. This library provides a fluent interface for working with JSON data, offering extensive functionality for parsing, manipulating, validating, and transforming JSON.
- π Modern PHP 8+ Support: Leverages latest PHP features including typed properties, union types, and attributes
- π‘οΈ Type-Safe Operations: Strong type checking and validation for reliable JSON handling
- π Fluent Interface: Chainable methods for intuitive JSON manipulation
- π JSON Schema Validation: Built-in support for JSON Schema validation
- π― Path Operations: JSONPath and Pointer support for precise data access
- π Advanced Querying: Complex data querying capabilities
- π οΈ Data Transformation: Rich set of transformation and mapping functions
- π Security Features: Protection against JSON vulnerabilities
- π Format Conversion: Convert between JSON and various formats (XML, YAML, CSV)
- π¨ Pretty Printing: Customizable JSON formatting options
- π Streaming Support: Efficient handling of large JSON files
- β‘ High Performance: Optimized for speed and memory efficiency
- π§ͺ Extensive Testing: Comprehensive test coverage
- π Rich Documentation: Detailed documentation with examples
- PHP 8.0 or higher
- ext-json
- ext-mbstring
Install the package via Composer:
composer require skpassegna/json-parser
use Skpassegna\Json\Json;
// Parse JSON string
$json = Json::parse('{"name": "John", "age": 30}');
// Access data
$name = $json->get('name'); // "John"
// Modify data
$json->set('age', 31)
->set('city', 'New York');
// Add nested data
$json->set('address', [
'street' => '123 Main St',
'city' => 'New York',
'country' => 'USA'
]);
// Convert back to JSON string
$jsonString = $json->toString();
// Pretty print
$prettyJson = $json->toString(Json::PRETTY_PRINT);
// Validate against schema
$isValid = $json->validateSchema($schema);
// Query using JSONPath
$results = $json->query('$.address.city');
use Skpassegna\Json\Json;
// Parse JSON string
$json = Json::parse('{"name": "John", "age": 30}');
// Access data
$name = $json->get('name'); // "John"
$age = $json->get('age'); // 30
// Modify data
$json->set('age', 31);
// Convert back to JSON
$jsonString = $json->toJson();
use Skpassegna\Json\Schema\Validator;
$schema = [
'type' => 'object',
'properties' => [
'name' => ['type' => 'string'],
'age' => ['type' => 'integer']
]
];
$json->validateSchema($schema); // Returns true/false
$json->validateSchemaWithErrors($schema); // Returns validation errors
// Transform JSON to XML
$xml = $json->toXml();
// Transform JSON to YAML
$yaml = $json->toYaml();
// Transform JSON to CSV
$csv = $json->toCsv();
use Skpassegna\Json\Stream\JsonReader;
use Skpassegna\Json\Stream\JsonWriter;
// Read large JSON file
$reader = new JsonReader('large-file.json');
foreach ($reader as $item) {
// Process each item
}
// Write large JSON file
$writer = new JsonWriter('output.json');
$writer->write($data);
// Sanitize JSON input
$json = Json::parse($input, ['sanitize' => true]);
// Maximum depth protection
$json = Json::parse($input, ['max_depth' => 10]);
// Maximum length protection
$json = Json::parse($input, ['max_length' => 1000000]);
The sunburst chart above provides a visual representation of code coverage across the project. The inner-most circle represents the entire project, with subsequent layers representing folders and files. The size and color of each slice indicate the number of statements and coverage percentage, respectively.
The library uses custom exceptions for different types of errors:
JsonException
: Base exception for all JSON-related errorsParseException
: JSON parsing errorsValidationException
: Schema validation errorsPathException
: JSONPath related errorsTransformException
: Data transformation errors
use Skpassegna\Json\Exceptions\JsonException;
try {
$json = Json::parse($invalidJson);
} catch (JsonException $e) {
echo $e->getMessage();
}
Contributions are welcome! Please see the CONTRIBUTING.md file for guidelines on how to contribute.
-
How do I handle errors? Ensure you catch exceptions when parsing or manipulating JSON data.
-
What PHP versions are supported? The library supports PHP 8.0 and higher.
For a list of changes and updates, please refer to the CHANGELOG.md.
composer test
The MIT License (MIT). Please see License File for more information.
If you discover any security related issues, please email [email protected] instead of using the issue tracker.