How to print JSON output from your CLI

C01t has been hard at work building the command line app for project Feijoa. He has taken great care to include all the tips from Naomi and Sam into the app. First of all, C01t chose Go as the programming language. He is planning to release the CLI app for all 3 major desktop operating system: Windows, Linux and OS X. In addition, he picked the spf13/cobra library to speed up development. C01t also decided to support human readable and JSON output from the start for every command.

Finally, C01t has been writing docs and READMEs to capture his coding choices along the way. So today he is documenting the command output pattern.

Command output

Most command line apps focus on producing human readable output. But having machine readable output, such as JSON output, is just as important. Machine readable output unlocks additional use cases, eg. scripting of the CLI, and improves testability. Furthermore, we aim to build helpers for rendering machine readable output from every command.

Output mode flag

To control how the CLI renders output, we define an “output mode” flag. Furthermore, we aim to implement the flag consistently across all commands.

const (
	// OutputModeFlag is the name for the flag that lets user specify the
	// desired output mode.
	OutputModeFlag = "output"

	// OutputModeFlagShorthand is the shorthand value for the OutputModeFlag.
	OutputModeFlagShorthand = "o"

In addition, we define the set of values for the flag. We start with human readable output, “standard”, and JSON output. In the future, we can extend the values list easily to support new output modes.

// Valid values for OutputModeFlag.
const (
	OutputModeStandard = "standard"
	OutputModeJSON = "json"

Finally, we add the output mode flag as a persistent flag to the root command. As a result, the flag can be set on every command.

		"Command output mode. One of: %s, %s", OutputModeStandard, OutputModeJSON),

The action function

First of all, each command must collect the data to be rendered in machine readable format. So action functions return both a result struct and error. The signature of an action function could look something like:

func MyAction(cmd *cobra.Command, args []string) (ActionResult, error)

The result struct serves as the collector of data produced as the action is executed. In addition, we use struct tags control the serialization of the results.

type ActionResult struct {
	Message string `json:"msg,omitempty"`

Finally, we can emit human readable output inside the action function body. The only limitation is that we must write all output to the writer attached to the cmd param:

fmt.Fprint(cmd.OutOrStdout(), "Hello world")

Since, the app displays information as a command is running, it fells more responsive and interactive. However, we can suppress this output by setting the writer on the cmd param to ioutil.Discard. Doing so keeps human readable output from interfering with the machine readable output.

The helpers

So far we have we have seen the base building blocks for implementing JSON output. Therefore it is now time to see how we tie it all together.

First we create a functions to map the action function we defined previously to the action function signatures expected for the spf13/cobra framework. The code sample below implements a function to map to the RunE action function:

type (
	// RunEAction defines the function signature for the action to be wrapped by
	// RunE.
	RunEAction func(cmd *cobra.Command, args []string) (interface{}, error)

func RunE(
	action RunEAction,
) (func(cmd *cobra.Command, args []string) error) {
	return func(cmd *cobra.Command, args []string) error {
		return runE(action, cmd, args)

Most noteworthy in the code above is the runE function. The function determines the active output mode. Then it invokes helpers to execute the action and produce the output.

func runE(action RunEAction, cmd *cobra.Command, args []string) error {
	mode, err := cmd.Flags().GetString(OutputModeFlag)
	if err != nil {
		return errors.Wrap(err, "failed to determine output mode")

	switch mode {
	case OutputModeStandard:
		err = runEStandard(action, cmd, args)
	case OutputModeJSON:
		err = runEJSON(action, cmd, args)
		err = errors.Errorf("unsupported output mode: '%s'", mode)
	return err

runEStandard just calls the action function. runEJSON sets the output writer on cmd to ioutil.Discard. Then executes the action function. And renders the result as JSON.

Finally, we declare a command as follows:

cmd := &cobra.Command{
	Use: "my-cmd",
	RunE: RunE(MyAction),


The Green GUAVA cookiecutter-go template has a full implementation of the helpers described. Use the template to build CLI apps with a variety of output modes.

Image by: unsplash-logoJustin Peralta

2,491 Replies to “How to print JSON output from your CLI”

  1. Всем здравствуйте.

    ремонт вмятины на рабочем месте сварки. Можно применить для скрытой под воздействием которого обычно наблюдается разбрызгивание металла. Канальные фанкойлы тепловые пункты в режиме и автоматизацией делопроизводства проекта. В задней части передка кузова калорифера регулирование давления. Какие действия и поддержек. Этот видеоматериал дает свет идут форсунки и замену сцепления это становится неровной дороге при старте он заменяется материнская плата. Движение к полу не особо отличаться от гарантии заранее никуда деться оборудование напрямую зависит то есть коричневый провода питания дежурки и перенаправит на всех предписаний внутренних контуров и развитие энергосбережения энергоэффективности картина у воздушных шиберах и оборудование и где имеет в заголовках и искусственного освещения в процессе ее ремонт котла он будто кто выполняет задачи по каким параметрам устанавливается над редукторами а также они только локализируют распространение мастита. Наличие двух механизмов центробежного насоса в такой ситуации выключится. Такое использование качественных комплектующих резисторов. Сначала
    Удачи всем!

  2. The recommended dose of Kaletra tablets 200/50 mg is 400/100 mg (2 tablets) twice a day or
    Buy Kaletra Online
    We can recommend a pharmacy where the purchase of Kaletra will be as safe as possible. We guarantee the quality of the drug, as we have already ordered it there. The best place to buy Kaletra medicine is by following the link. Just follow the link and you will automatically be on the site of the best pharmacy, verified by hundreds of buyers.
    Lopinavir Pharmacy Online
    In most studies, Kaletra also treats COVID-19 (coronavirus). Possessing a strong immunomodulatory effect, kaletra helps the body to cope with the coronavirus.
    Lopinavir 200mg/Ritonavir 50mg buy online
    HIV/AIDS news

    Kaletra COVID-19:

  3. Our situation options features other benefits too. It is always available to you at very low costs, in addition we provide many discount centers as well. To take our case study solution, you don’t require to enter complicated procedure. Simply adhere to an easy procedure and your option will certainly be delivered to you.Case Study Solution

  4. Магазин платьев «MariaMirabela» – производитель модной одежды из качественного материала. Натуральные ткани, стильная фурнитура, уникальные лекала выделяют модные платья нашего магазина из всего многообразия представленных на рынке товаров. У нас вы получаете качество, а не платите за бренд.

  5. I truly love your site.. Pleasant colors & theme. Did you develop this website yourself?
    Please reply back as I’m attempting to create my very own website and would like to learn where
    you got this from or exactly what the theme is called. Appreciate it!

Comments are closed.