Home > Enterprise >  Transform file content into json string with jq doesn't work in command substitution
Transform file content into json string with jq doesn't work in command substitution

Time:09-16

This is working as expected:

$ cat /etc/tfe-config/sources/fluent-bit.conf.tpl | jq -R -s
$ "[OUTPUT]\n    Name               cloudwatch_logs\n    Match              *\n    region             eu-central-1\n    log_group_name     TFE-LogForwarding\n    log_stream_name    TFE-AllLogs"

However, assignment to a variable does not work:

$ MY_VARIABLE=$(cat /etc/tfe-config/sources/fluent-bit.conf.tpl | jq -R -s)
$ echo $MY_VARIABLE 
jq - commandline JSON processor [version 1.5]
Usage: jq [options] <jq filter> [file...]

        jq is a tool for processing JSON inputs, applying the
        given filter to its JSON text inputs and producing the
        filter's results as JSON on standard output.
        The simplest filter is ., which is the identity filter,
        copying jq's input to its output unmodified (except for
        formatting).
        For more advanced filters see the jq(1) manpage ("man jq")
        and/or https://stedolan.github.io/jq

        Some of the options include:
         -c             compact instead of pretty-printed output;
       .... trimmed

I am on AWS EC2 machine with the latest Amazon Linux 2 image.

What is going on here?

The file looks like this:

[OUTPUT]
    Name               cloudwatch_logs
    Match              *
    region             eu-central-1
    log_group_name     TFE-LogForwarding
    log_stream_name    TFE-AllLogs

CodePudding user response:

Two things:

  • You have to specify a filter for jq – just . to get the entire input
  • Once in a variable with whitespace, you must quote the string, else it shows up differently when you print it
var=$(cat /etc/tfe-config/sources/fluent-bit.conf.tpl | jq -R -s '.')
echo "$var" 

Relevant Q&A:

  • Related