Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't store output of jq in variable

Tags:

json

bash

jq

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

like image 492
Daryl Van Sittert Avatar asked Jan 04 '18 06:01

Daryl Van Sittert


People also ask

How do you use a variable inside jq?

Using JSON Variables in jq If you want to use variables that reference JSON objects, this can be done with the --argsjson option. Using --argjson var object will set the variable $var to object . In the example below we set the $location variable to a JSON object, and nest this object into our results.

What is the output of jq?

jq usually outputs non-ASCII Unicode codepoints as UTF-8, even if the input specified them as escape sequences (like "\u03bc"). Using this option, you can force jq to produce pure ASCII output with every non-ASCII character replaced with the equivalent escape sequence.

What does jq do in bash?

jq command is used not only for reading JSON data but also to display data by removing the particular key. The following command will print all key values of Students. json file by excluding batch key. map and del function are used in jq command to do the task.

What is jq JSON?

jq is a lightweight and flexible command-line JSON processor. If you are a command line addict, you will like the official description. jq is like sed for JSON data – you can use it to slice and filter and map and transform structured data with the same ease that sed, awk, grep and friends let you play with text.


1 Answers

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

 jq -s -R .
like image 179
peak Avatar answered Oct 28 '22 06:10

peak