softcreatr/jsonpath
Evaluate and query JSON with JSONPath in PHP. softcreatr/jsonpath lets you select, filter, and extract data from arrays/objects using familiar JSONPath expressions, making it easy to navigate complex nested structures for APIs, configs, and fixtures.
Start by requiring the package via Composer:
composer require softcreatr/jsonpath
Then, instantiate the JSONPath parser with your JSON (or decoded array). The simplest use case is extracting nested data:
use Softcreatr\JsonPath\JsonPath;
$json = '{"users": [{"name": "Alice", "role": "admin"}, {"name": "Bob", "role": "user"}]}';
$jp = new JsonPath();
$results = $jp->find($json, '$.users[*].name'); // Returns ['Alice', 'Bob']
Check the README for syntax basics and start with basic dot notation ($.key) and wildcard ($[*]) selectors.
array_merge or nested loops.collect() to map filtered results:
$paths = $jp->find($json, '$.items[?(@.status == "active")]');
$ids = collect($paths)->pluck('id')->all();
assertJsonPath($json, '$.data.id', 123).JsonPath as a singleton in a service provider for reuse across your app, especially in job handlers processing external payloads.$[?(@.score > 10)]), not PHP-like syntax. Comparison operators are strict (==, !=, >, <, etc.), and strings need quotes.findAll() to get both values and paths (e.g., ['$.users[0].name']), useful for debugging or deep structural mapping.$jp->find($json, '$.missing') returns an empty array—not null. Guard against this in conditions with empty($results).\Softcreatr\JsonPath\JsonPath to add domain-specific helpers (e.g., findActiveUsers()) that pre-apply common filters.JsonPath instances (it’s stateless, so safe for reuse).How can I help you explore Laravel packages today?