🌳 ParseDependencyTreeJSON
Parses the JSON output of composer show --tree --format=json and returns a list of dependency tree nodes []DependencyNode.
When to use
Use this when you already have the JSON-format dependency tree output and need to programmatically traverse a package's transitive dependency relationships. Compared to text parsing, the JSON version reliably expresses parent-child hierarchy.
Signature
go
func ParseDependencyTreeJSON(output string) ([]DependencyNode, error)Parameters
| Parameter | Type | Description |
|---|---|---|
output | string | The raw output of composer show --tree --format=json |
Return value
[]DependencyNode: the list of dependency tree root nodes. EachDependencyNodecontainsName,Version, andChildren(recursive child nodes).error: returns afailed to parse dependency tree JSON: ...error when JSON deserialization fails.
Example
go
package main
import (
"fmt"
"log"
"github.com/scagogogo/composer-skills/pkg/composer"
)
func main() {
output := `[{"name":"symfony/console","version":"v5.4.0","children":[{"name":"symfony/string","version":"v6.0.0"}]}]`
nodes, err := composer.ParseDependencyTreeJSON(output)
if err != nil {
log.Fatal(err)
}
for _, n := range nodes {
printNode(n, 0)
}
}
func printNode(n composer.DependencyNode, depth int) {
for i := 0; i < depth; i++ {
fmt.Print(" ")
}
fmt.Printf("%s %s\n", n.Name, n.Version)
for _, c := range n.Children {
printNode(c, depth+1)
}
}Advanced
- 📝 If you have the text output of
composer show --tree(without--format=json), useParseDependencyTreeOutput(output)instead, which infers parent-child relationships by indentation level (one level per 4 characters). - 🚀 To obtain a text dependency tree, use
ShowDependencyTree(packageName). - 🔗 For the related parser in the same family, see
ParseComposerShowJSON.