5

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...]

1

1 Answer 1

10

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

 jq -s -R .
2
  • That works. Thanks! But I don't understand why it didn't work without the dot when I tried to capture the output of the subshell. It even worked when I put parenthesis around without the dollar. Jan 4, 2018 at 10:53
  • 2
    Those not clearly documented, it appears you can only omit the . when jq can detect its standard output is a terminal.
    – chepner
    Jan 8, 2018 at 21:22

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.