- Can I use peekmo/jsonpath in Laravel 9/10 for API response parsing?
- Yes, but with caveats. The package works in Laravel as a standalone utility, but it lacks PHP 8.x support and may conflict with Laravel’s built-in `json_path()` helper in newer versions. Test thoroughly for edge cases like nested arrays or malformed JSON. For modern Laravel apps, consider alternatives like `humbug/jsonpath` for RFC 8907 compliance.
- Does peekmo/jsonpath support recursive descent (e.g., `$.store..book`)?
- No, this package does not natively support recursive descent (`..`). If you need deep recursive queries, you’ll either need to pre-process the JSON manually or switch to a modern alternative like `stevegrunwell/jsonpath`, which includes this feature. The package focuses on basic JSONPath selectors like child access (`$.parent.child`) and array indexing.
- How do I install peekmo/jsonpath in a Laravel project?
- Add it via Composer: `composer require peekmo/jsonpath`. No Laravel-specific setup is required, but wrap usage in a service class to isolate dependencies. For example, create a `JsonPathService` facade to centralize queries and handle potential deprecation warnings. Avoid direct `JsonPath::query()` calls in controllers for better maintainability.
- Is peekmo/jsonpath safe for untrusted JSON input (e.g., API responses)?
- No, this package does **not** validate JSON input against injection risks (e.g., malformed paths like `$.store..book[?(@.price > $)]`). Always sanitize or decode JSON with `json_decode($input, true)` before passing it to the package. For production, consider adding input validation or using a modern library with built-in safety checks.
- What Laravel versions does peekmo/jsonpath support?
- The package itself is framework-agnostic but requires **PHP 5.3–5.6**, which limits Laravel compatibility. It won’t work natively in Laravel 8+ (PHP 8.x) due to deprecated functions like `create_function()`. For Laravel 5.6 or earlier, it may function, but test thoroughly. For newer Laravel versions, use a PHP 8.x-compatible alternative.
- How do I handle array slicing (e.g., `$.items[0:2]`) with this package?
- peekmo/jsonpath does **not** support array slicing syntax like `[start:end]`. You’ll need to manually slice arrays in PHP after querying (e.g., `array_slice(JsonPath::query($json, '$.items'), 0, 2)`). For advanced slicing, consider `stevegrunwell/jsonpath`, which includes this feature as part of its JSONPath 1.0 support.
- Are there any known performance issues with deep JSON nesting?
- Performance hasn’t been optimized for deeply nested JSON structures. Simple queries should work fine, but complex paths (e.g., 10+ levels deep) may be slower than modern implementations like `humbug/jsonpath`, which includes optimizations for recursive traversal. Benchmark against alternatives if performance is critical.
- What alternatives should I consider if peekmo/jsonpath is outdated?
- For modern Laravel apps, prioritize `humbug/jsonpath` (PHP 8.x, RFC 8907 compliant) or `stevegrunwell/jsonpath` (feature-rich, active maintenance). If you need a lightweight solution but still want recursive queries, `mtdowling/jsonpath` (though less maintained) is another option. Avoid this package for new projects due to its archived status and lack of PHP 8.x support.
- Can I use peekmo/jsonpath with Laravel’s built-in JSON helpers (e.g., `json_path()`)?
- No, these are separate tools. Laravel’s `json_path()` (introduced in Laravel 10+) is a wrapper around `humbug/jsonpath` and won’t interact with `peekmo/jsonpath`. If you mix them, you’ll need to choose one consistently. For consistency, stick to either the package’s API or Laravel’s helper, but avoid combining them in the same query logic.
- How do I test peekmo/jsonpath in a Laravel application?
- Since the package lacks built-in tests, manually validate edge cases: empty arrays, circular references, and malformed JSON. Use PHPUnit to test your service layer (e.g., `JsonPathService`) with mock JSON inputs. For example, test `$.user.*` on nested objects and verify array indexing (`$.items[0]`) works as expected. Document any limitations in your test suite.