Skip to content

🔍 ParseComposerShowJSON

Parses the JSON output of composer show <package> --format=json and returns the structured PackageInfo.

When to use

Use this when you already have the raw JSON output string of composer show and need to convert it into a Go struct to programmatically read fields such as package name, version, dependencies, and authors. It is the parser called internally by ShowPackageInfo.

Signature

go
func ParseComposerShowJSON(output string) (*PackageInfo, error)

Parameters

ParameterTypeDescription
outputstringThe raw output of composer show <package> --format=json

Return value

  • *PackageInfo: the package info struct, containing Name, Version, Description, Authors, Require, Source, Dist, and other fields.
  • error: returned when JSON deserialization fails (internally delegates to ParsePackageInfo).

Example

go
package main

import (
	"fmt"
	"log"

	"github.com/scagogogo/composer-skills/pkg/composer"
)

func main() {
	output := `{"name":"symfony/console","version":"v5.4.0","description":"Symfony Console Component"}`

	info, err := composer.ParseComposerShowJSON(output)
	if err != nil {
		log.Fatal(err)
	}
	fmt.Printf("%s %s\n", info.Name, info.Version)
}

Advanced

  • 🔄 This function is equivalent to ParsePackageInfo(output); the two are interchangeable.
  • 🚀 To execute the command and parse in one step, use ShowPackageInfo(packageName), which internally runs Run("show", pkg, "--format", "json") and then calls this parser.
  • 📋 Related parsers in the same family: ParseComposerOutdatedJSON, ParseComposerAuditJSON, ParseDependencyTreeJSON.

Released under the MIT License