sy extract --help
sy-extract 
Extract scalar or complex values from any JSON or YAML file. Multi-document YAML files are supported.

USAGE:
    sy extract [FLAGS] [OPTIONS] <pointer>...

FLAGS:
    -h, --help        Prints help information
        --no-stdin    If set, we will not try to read structured data from standard input. This may be required in some
                      situations where we are blockingly reading from a standard input which is attached to a pseudo-
                      terminal.

OPTIONS:
    -f, --file=<file>...    The path to the file to include, or '-' to read from standard input. It must be in a format
                            that can be output using the --output flag.
    -o, --output=<mode>     Specifies how the extracted result should be serialized. If the output format is not
                            explicitly set, the output will be a single scalar value per line. If the output contains a
                            complex value, the default serialization format will be used. [possible values: json, yaml]

ARGS:
    <pointer>...    Use a JSON pointer to specify which value to extract. Valid specifications are for example
                    '0/a/b/4' or 'a.b.0', and they must point to a valid value.

You can also use this alias: fetch.

The extract sub-command is meant for those cases when you need individual values from a file with structured data, for example when consuming credentials in scripts.

When extracting scalar values, those will be output one per line. If an extracted value is not scalar, e.g. an object or array, the output mode of all extracted values will change to JSON (default) or YAML with --output.

All values are specified using JSON-pointer notation, with the added convenience that slashes (-) can be exchanged with dots (.) .

Extracting username and password

Given an input file like this, here is how you can extract username and password for usage in scripts:

cat credentials.yml
user:
  name: Hans
  password: geheim

Extract using the JSON pointer syntax is quite straightforward, and rather forgiving:

sy extract -f=credentials.yml user.name /user/password
Hans
geheim

From here it should be easy to assign individual values to variables for use in scripts

password="$(sy extract user.password < credentials.yml)"
username="$(sy extract user.name < credentials.yml)"
echo Authorization: Basic $(echo $username:$password | base64)
Authorization: Basic SGFuczpnZWhlaW0K

Collecting individual values into a structured format

By default, and as long as you are not extracting a non-sclar value, the output will be a single line per value. Otherwise, you will either get a list of JSON or YAML values.

sy extract user.name user/password -o json < credentials.yml
[
  "Hans",
  "geheim"
]