Daryl Van Sittert
Daryl Van Sittert

Reputation: 877

Can't store output of jq in variable

I have some text that I want to escape to something I can use in JSON. I can escape the text using jq and display it

normaltext="My normal text that I want to put in \"JSON\""
echo $normaltext | jq --slurp --raw-input

"My normal text that I want to put in \"JSON\"\n"

However, store that command output into a variable, jq doesn't seem to receive the input and just displays the help text.

escapedtext=$(echo $normaltext | jq --slurp --raw-input)

jq - commandline JSON processor [version 1.5-1-a5b5cbe] Usage: jq [options] [file...]

Upvotes: 5

Views: 4591

Answers (1)

peak
peak

Reputation: 116730

Your version of jq evidently requires the . filter here, as in:

 jq -s -R .

Upvotes: 10

Related Questions