AgentSkillsCN

symfony-7-4-json-path

提供关于如何使用Symfony JsonPath组件,通过RFC 9535 JSONPath表达式查询并提取JSON结构中数据的指导。触发器包括:JsonPath、JSON查询、JSON数据提取、JSONPath表达式、JsonCrawler。

SKILL.md
--- frontmatter
name: "symfony-7-4-json-path"
description: "Provides guidance on using the Symfony JsonPath component for querying and extracting data from JSON structures using RFC 9535 JSONPath expressions. Triggers on: JsonPath, JSON querying, JSON data extraction, JSONPath expressions, JsonCrawler."

Symfony 7.4 JsonPath Component

Overview

The Symfony JsonPath component allows querying and extracting data from JSON structures using the RFC 9535 JSONPath standard. It provides JsonCrawler for executing queries and JsonPath for building queries programmatically.

Quick Reference

Installation

bash
composer require symfony/json-path

Core Usage

php
use Symfony\Component\JsonPath\JsonCrawler;

$crawler = new JsonCrawler($jsonString);
$results = $crawler->find('$.store.book[0].title');

Programmatic Query Building

php
use Symfony\Component\JsonPath\JsonPath;

$path = (new JsonPath())
    ->key('store')
    ->key('book')
    ->index(0)
    ->key('title');

$results = $crawler->find($path);

JsonPath Builder Methods

MethodDescription
key($name)Select a named key (auto-escaped)
deepScan()Recursive descent (..)
all()Wildcard ([*])
index($n)Array index
first()Shortcut for index(0)
last()Shortcut for index(-1)
slice($start, $end, $step)Array slice
filter($expr)Filter expression

Common Query Patterns

PatternExpression
Root property$.store
Nested property$.store.book[0].title
All items$.store.book[*]
Recursive descent$..author
Filter by value$.store.book[?(@.price < 10)]
Bracket notation$["store"]["book"][0]

PHPUnit Assertions

php
use Symfony\Component\JsonPath\Test\JsonPathAssertionsTrait;

self::assertJsonPathCount(2, '$.books[*]', $json);
self::assertJsonPathSame('expected', '$.path', $json);
self::assertJsonPathContains('value', '$.path', $json);

Full Documentation

References