phpdocumentor/json-path
PHP library from phpDocumentor that adds JSONPath query support for selecting and extracting data from JSON structures. Useful for tooling and document generation workflows needing simple path-based access to nested JSON.
composer require phpdocumentor/json-path
$data = json_decode($jsonString, true);
use phpDocumentor\JsonPath\JsonPath;
$jsonPath = new JsonPath();
$results = $jsonPath->find($data, '$.store.book[0].title');
// Returns: ['The Great Gatsby'] (array of matches)
$.results[?(@.status == 'active')].id.$activeUsers = $jsonPath->find($data, '$.users[?(@.active == true)]');
$allPrices = $jsonPath->find($orderData, '$.items[*].price');
&&):
$premiumUsers = $jsonPath->find($users, '$.users[?(@.tier == "premium" && @.country == "US")]');
$value = $jsonPath->find($config, '$.database.host')[0] ?? null;
function json_get(string $path, array $data): mixed {
return (new JsonPath())->find($data, $path);
}
json_decode() first — passing strings causes TypeError.$ prefix is required (e.g., $.a.b); using a.b silently returns [].[]), not null — guard with null coalescing or isset().$data structure — the class has no internal caching.$data (e.g., normalize timestamps to ISO strings) before querying.json_encode($data) before querying — mismatched types (e.g., "123" vs 123) cause filters to fail unexpectedly.How can I help you explore Laravel packages today?