1. Go to this page and download the library: Download cerbero/json-parser library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?phprequire_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
cerbero / json-parser example snippets
// a source is anything that can provide a JSON, in this case an endpoint
$source = 'https://randomuser.me/api/1.4?seed=json-parser&results=5';
foreach (new JsonParser($source) as $key => $value) {
// instead of loading the whole JSON, we keep in memory only one key and value at a time
}
JsonParser::parse($source)->traverse(function(mixed $value, string|int $key, JsonParser $parser){
// lazily load one key and value at a time, we can also access the parser if needed
});
// no foreach needed
useCerbero\JsonParser\Sources\Source;
useTraversable;
classCustomSourceextendsSource{
publicfunctiongetIterator(): Traversable{
// return a Traversable holding the JSON source, e.g. a Generator yielding chunks of JSON
}
publicfunctionmatches(): bool{
// return TRUE if this class can handle the JSON source
}
protectedfunctioncalculateSize(): ?int{
// return the size of the JSON in bytes or NULL if it can't be calculated
}
}
$json = JsonParser::parse(new CustomSource($source));
foreach ($json as $key => $value) {
// process one key and value of $source at a time
}
$json = JsonParser::parse($source)->pointer('/results/0/gender');
foreach ($json as $key => $value) {
// 1st and only iteration: $key === 'gender', $value === 'female'
}
$json = JsonParser::parse($source)->pointer('/results/-/gender');
foreach ($json as $key => $value) {
// 1st iteration: $key === 'gender', $value === 'female'// 2nd iteration: $key === 'gender', $value === 'female'// 3rd iteration: $key === 'gender', $value === 'male'// and so on for all the objects in the array...
}
$json = JsonParser::parse($source)->pointers(['/results/-/gender', '/results/-/location/country']);
foreach ($json as $key => $value) {
// 1st iteration: $key === 'gender', $value === 'female'// 2nd iteration: $key === 'country', $value === 'Germany'// 3rd iteration: $key === 'gender', $value === 'female'// 4th iteration: $key === 'country', $value === 'Mexico'// and so on for all the objects in the array...
}
$json = JsonParser::parse($source)->pointers([
'/results/-/gender' => fn (string $gender, string $key) => new Gender($gender),
'/results/-/location/country' => fn (string $country, string $key) => new Country($country),
]);
foreach ($json as $key => $value) {
// 1st iteration: $key === 'gender', $value instanceof Gender// 2nd iteration: $key === 'country', $value instanceof Country// and so on for all the objects in the array...
}
$json = JsonParser::parse($source)
->pointer('/results/-/gender', fn (string $gender, string $key) => new Gender($gender))
->pointer('/results/-/location/country', fn (string $country, string $key) => new Country($country));
foreach ($json as $key => $value) {
// 1st iteration: $key === 'gender', $value instanceof Gender// 2nd iteration: $key === 'country', $value instanceof Country// and so on for all the objects in the array...
}
$json = JsonParser::parse($source)->pointer('/results/-/name/first', function(string $name, string &$key){
$key = 'first_name';
});
foreach ($json as $key => $value) {
// 1st iteration: $key === 'first_name', $value === 'Sara'// 2nd iteration: $key === 'first_name', $value === 'Andrea'// and so on for all the objects in the array...
}
JsonParser::parse($source)
->pointer('/-/gender', $this->handleGender(...))
->pointer('/-/location/country', $this->handleCountry(...))
->traverse();
// no foreach needed
JsonParser::parse($source)
->pointer('/results/-/gender', fn (string $gender, string $key) => new Gender($gender))
->pointer('/results/-/location/country', fn (string $country, string $key) => new Country($country))
->traverse(function(Gender|Country $value, string $key, JsonParser $parser){
// 1st iteration: $key === 'gender', $value instanceof Gender// 2nd iteration: $key === 'country', $value instanceof Country// and so on for all the objects in the array...
});
// no foreach needed
useCerbero\JsonParser\Decoders\Decoder;
useCerbero\JsonParser\Decoders\DecodedValue;
classCustomDecoderimplementsDecoder{
publicfunctiondecode(string $json): DecodedValue{
// return an instance of DecodedValue both in case of success or failure
}
}
useCerbero\JsonParser\Decoders\AbstractDecoder;
classCustomDecoderextendsAbstractDecoder{
protectedfunctiondecodeJson(string $json): mixed{
// decode the given JSON or throw an exception on failurereturn json_decode($json, flags: JSON_THROW_ON_ERROR);
}
}
useCerbero\JsonParser\Exceptions\JsonParserException;
try {
JsonParser::parse($source)->traverse();
} catch (JsonParserException) {
// handle any exception thrown by JSON Parser
}
$json = new JsonParser($source);
$json->progress(); // <Cerbero\JsonParser\ValueObjects\Progress>
$json->progress()->current(); // the already parsed bytes e.g. 86759341
$json->progress()->total(); // the total bytes to parse e.g. 182332642
$json->progress()->fraction(); // the completed fraction e.g. 0.47583
$json->progress()->percentage(); // the completed percentage e.g. 47.583
$json->progress()->format(); // the formatted progress e.g. 47.5%