JSON and YAML graph definitions
A graph definition can live in a data file that your application loads when it needs the graph. That lets you review changes to nodes, choices and presentation separately from the PHP code that runs them. JSON and YAML can describe the same definition; choosing between them mainly affects how people edit the file and how other tools exchange it.
This guide reuses the learning-path graph from Anatomy of a graph. A decision at node 10 leads to the guide through edge 101 or to practice through edge 102. Practice requires practiceEnabled: true in the session context. Both complete files linked below contain the same nodes, conditions, English and Spanish presentation, and terminal outputs.
Choose a format for the people and tools maintaining it
Section titled “Choose a format for the people and tools maintaining it”JSON works well when software generates or exchanges the definition. Its syntax makes strings, numbers, booleans, objects and lists explicit, and the same representation fits naturally into JSON-based tooling.
YAML can be more comfortable for definitions edited by hand. It allows comments and uses indentation to express nested data, so a condition can be read with fewer brackets and quotes. That convenience also makes whitespace and scalar types part of the editing task: indentation changes where data belongs, and quotes can change its type.
| If your definition is mainly… | A practical choice | What to keep in mind |
|---|---|---|
| Generated or exchanged by other software | JSON | Generate valid JSON; comments and trailing commas are not part of its syntax. |
| Maintained by people who want comments beside decisions | YAML | Keep indentation consistent and distinguish text from booleans and numbers. |
| Shared between tools with different format preferences | Either, with an explicit conversion | Check that the conversion preserves values, types and list order. |
These choices do not give the graph different capabilities. Once imported, both representations become a Graph; the engine receives nodes, edges and associated data rather than JSON or YAML syntax.
Represent the same condition
Section titled “Represent the same condition”In the learning-path graph, the following JSON record attaches a condition to edge 102. This is an excerpt from the complete definition:
{ "conditions": [ { "edge_id": 102, "rule": "context_value", "params": { "key": "practiceEnabled", "operator": "==", "value": true } } ]}The same excerpt in YAML is:
conditions: - edge_id: 102 rule: context_value params: key: practiceEnabled operator: '==' value: trueThe YAML hyphen creates a list item. The indentation places key, operator and value inside params. After parsing, these are the same keys and values as in the JSON record: integer 102, string '==' and boolean true.
The rule reads practiceEnabled from the run’s context. Neither file assigns that value to the session. Your application still supplies the context, and the engine compares it with the condition declared in the file.
Load JSON directly
Section titled “Load JSON directly”In a PHP project with the Composer autoloader loaded, JsonGraphLoader accepts either a path to a JSON file or a string containing JSON:
use ExecKernel\Infrastructure\Loader\JsonGraphLoader;
$jsonGraph = (new JsonGraphLoader())->load( __DIR__ . '/learning-path.json',);For this file, load() reads the JSON, decodes its data, validates the graph and returns a Graph. The application can pass $jsonGraph to the engine’s start() method. It does not need to turn each JSON node or label into an object itself.
The file name locates the source; the graph_code inside the definition identifies the graph. Renaming learning-path.json does not change the stored identity learning-path.
Parse YAML, then import the resulting array
Section titled “Parse YAML, then import the resulting array”For YAML, use the Symfony YAML component included in ExecKernel’s Composer dependencies. Its parser turns the YAML document into PHP data. SchemaImporter then interprets that data as an ExecKernel graph definition.
use ExecKernel\Infrastructure\Importer\SchemaImporter;use Symfony\Component\Yaml\Yaml;
$definition = Yaml::parseFile(__DIR__ . '/learning-path.yaml');
if (!is_array($definition)) { throw new UnexpectedValueException( 'The YAML document must contain a graph mapping.', );}
$importer = new SchemaImporter(strict: true);$yamlGraph = $importer->importJson($definition);parseFile() handles YAML syntax and scalar types. The array check rejects documents that decode to a single scalar; the importer then checks whether the array has the required graph structure and valid references.
The name importJson() refers to the graph’s data representation. Its argument is an already decoded PHP array, so it can also receive data parsed from YAML. It returns a Graph, just like the JSON loading path. Here, strict: true additionally requires presentation in the default language and checks known condition types and their parameters before the graph is used.
The complete path matters when you integrate file loading:
| Source | Component reading the source | Input to the graph importer or loader | Result |
|---|---|---|---|
| JSON file or JSON string | JsonGraphLoader |
File path or JSON text | Graph |
| YAML file | Yaml::parseFile(), followed by SchemaImporter |
PHP array passed to importJson() |
Graph |
| JSON or YAML file checked with the CLI | The CLI reads and parses the document, then calls SchemaImporter |
Parsed PHP array | A validation report |
Your application therefore owns the YAML parsing step when it loads YAML through PHP. The CLI already performs that step for its file commands.
Preserve types when editing
Section titled “Preserve types when editing”A representation can be syntactically valid and still change what the graph means. For the practice condition, these YAML values differ:
value: truevalue: "true"The first becomes a boolean; the second becomes a string. The context_value rule’s == comparison requires the value and type to match. If your application supplies the boolean true, only the first condition enables practice. The quoted version is a valid condition that asks a different question, so validation cannot infer that you intended a boolean.
Node and edge IDs also have types. Keep root_node_id: 10 and edge_id: 102 as integers. Quoting an ID turns it into a string and can make the definition fail graph validation.
When a file fails to load, first locate the stage that rejected it:
| Problem | Where it is detected | What to correct |
|---|---|---|
| Invalid JSON syntax | JSON decoding in the loader | Repair the JSON, such as a missing comma or an extra trailing comma. |
| Invalid YAML syntax | Symfony YAML parsing | Repair the YAML structure or quoting. |
| A valid document points an edge at a missing node | Graph import and validation | Correct the reference or add the intended node. |
A condition contains "true" when the application supplies true |
It may load successfully, then evaluate differently | Align the declared value and the application context type. |
In PHP, YAML parsing errors come from the YAML parser; graph errors come from ExecKernel after parsing. Keeping that distinction in your error handling helps an editor fix the right part of a definition.
Validate either file with the CLI
Section titled “Validate either file with the CLI”The CLI accepts both formats and applies strict graph validation with validate:
vendor/bin/execkernel validate learning-path.json --jsonvendor/bin/execkernel validate learning-path.yaml --jsonBoth complete files produce a successful result with three nodes, two edges, two terminal nodes and no warnings. In the JSON report, ok: true confirms success and summary contains those counts. The commands return exit code 0.
This checks the definition before the application starts a run. It does not establish that the business decision expressed by a condition is the one you intended. Graph validation and diagnostics explains how to work from a diagnostic to a correction.
Complete equivalent definitions
Section titled “Complete equivalent definitions”Download learning-path.json or learning-path.yaml. These files describe the same complete graph, including both languages and the terminal results.
load-learning-path.php contains the two loading paths above and compares the resulting definitions. To run it in an existing Composer project, place the script and both data files beside vendor/. Its result confirms that they describe the same three-node, two-edge graph. The script is a compact example of loading; your application can use the resulting Graph in its normal execution code.
For the full field contract, consult the graph definition schema. If you prefer declarations such as node, edge and when for authoring the definition, see FlowDSL.