Library for building interactive terminal inputs by using JSON schema.


How this works

Library can be used to build interactive mode for golang terminal applications like Cobra ( etc. Library uses JSON schema as specification for questions and survey go library to execute them ( As result developers will get JSON files containing answers from the user that conform to specified JSON schema.

In typical execution library will:

  • Read and process input JSON Schema file
  • Traverse JSON schema to create number of questions expecting specific types as answer
  • Validate user input against schema spec
  • Merge supplied defaults with the answers provided by users

Usage as CLI

To showcase how library works it provides an CLI that does accept JSON schema as file input.

To run cli with your local json schema execute

go get
askjschema --file=jsonschema.json

You can also use remote json schema

 askjschema --file

CLI binary files are also available from Github releases.

Usage as library

Add library to dependencies

go get

Add this sample code to your cobra handler method


    // Creates JSONSchema based of
    options := surveyjson.JSONSchemaOptions{
        Out:                 os.Stdout,
        In:                  os.Stdin,
        OutErr:              os.Stderr,
        AskExisting:         true,
        AutoAcceptDefaults:  false,
        NoAsk:               false,
        IgnoreMissingValues: false,

    yourSchema := ""
    initialValues := make(map[string]interface{})
    result, err := options.GenerateValues(yourSchema, initialValues)
    if err != nil {
        return err
    fmt.Fprint(os.Stdin, string(result))

For fully functional example see pkg/cmd/ask.go file


Supported JSON Schema properties

  • String (including specific formating for dates, emails etc.)
  • Boolean
  • Number
  • Null
  • Object
  • Enum
  • AllOf
  • If

Defaults from environment variables

Library can fetch default values for properties from environment variables. Env var should be constructed as SURVEY_VALUE_{JSON_SCHEMA_PATH}. For example:



View Github