Answer the question
In order to leave comments, you need to log in
What is a JSON parser in PHP?
We need a small independent PHP library for JSON parsing, which could indicate the location of the error in JSON.
Answer the question
In order to leave comments, you need to log in
In the example, invalid json causes an error on the line
<?php
// Get the schema and data as objects
$retriever = new JsonSchema\Uri\UriRetriever;
$schema = $retriever->retrieve('file://' . realpath('schema.json'));
$data = json_decode(file_get_contents('data.json'));
// If you use $ref or if you are unsure, resolve those references here
// This modifies the $schema object
$refResolver = new JsonSchema\RefResolver($retriever);
$refResolver->resolve($schema, 'file://' . __DIR__);
// Validate
$validator = new JsonSchema\Validator();
$validator->check($data, $schema);
if ($validator->isValid()) {
echo "The supplied JSON validates against the schema.\n";
} else {
echo "JSON does not validate. Violations:\n";
foreach ($validator->getErrors() as $error) {
echo sprintf("[%s] %s\n", $error['property'], $error['message']);
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question