Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Json Path Laravel Package

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.

View on GitHub
Deep Wiki
Context7

Getting Started

  1. Install via Composer:
    composer require phpdocumentor/json-path
    
  2. Decode your JSON first — the library works on PHP arrays/objects, not raw JSON strings:
    $data = json_decode($jsonString, true);
    
  3. Instantiate and query using a minimal API:
    use phpDocumentor\JsonPath\JsonPath;
    
    $jsonPath = new JsonPath();
    $results  = $jsonPath->find($data, '$.store.book[0].title');
    // Returns: ['The Great Gatsby'] (array of matches)
    
  4. First use case: Extract test fixtures from API responses — e.g., $.results[?(@.status == 'active')].id.

Implementation Patterns

  • Filter expressions for dynamic selection:
    $activeUsers = $jsonPath->find($data, '$.users[?(@.active == true)]');
    
  • Wildcard indexing for flexible array traversal:
    $allPrices = $jsonPath->find($orderData, '$.items[*].price');
    
  • Nested filtering for complex conditions (logical AND is supported via &&):
    $premiumUsers = $jsonPath->find($users, '$.users[?(@.tier == "premium" && @.country == "US")]');
    
  • Command-line tooling integration: Embed in scripts (e.g., CLI config validators):
    $value = $jsonPath->find($config, '$.database.host')[0] ?? null;
    
  • Laravel-style helper: Wrap in a reusable utility:
    function json_get(string $path, array $data): mixed {
        return (new JsonPath())->find($data, $path);
    }
    

Gotchas and Tips

  • No raw JSON input: Always json_decode() first — passing strings causes TypeError.
  • Root-only queries: The $ prefix is required (e.g., $.a.b); using a.b silently returns [].
  • Indexed vs. associative arrays: JSONPath treats all arrays as zero-indexed lists — indices never map to associative keys.
  • Empty results: Failed queries return an empty array ([]), not null — guard with null coalescing or isset().
  • Performance: For repeated queries on large documents, cache the $data structure — the class has no internal caching.
  • Extension points: The library uses a simple recursive visitor pattern — for advanced filtering, pre-process $data (e.g., normalize timestamps to ISO strings) before querying.
  • Debugging: Log raw json_encode($data) before querying — mismatched types (e.g., "123" vs 123) cause filters to fail unexpectedly.
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
davejamesmiller/laravel-breadcrumbs
artisanry/parsedown
christhompsontldr/phpsdk
enqueue/dsn
bunny/bunny
enqueue/test
enqueue/null
enqueue/amqp-tools
bower-asset/punycode
bower-asset/inputmask
bower-asset/jquery
bower-asset/yii2-pjax
laravel/nova
spatie/laravel-mailcoach
spatie/laravel-superseeder
laravel/liferaft
nst/json-test-suite
danielmiessler/sec-lists
jackalope/jackalope-transport
twbs/bootstrap4