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...]
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.
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.
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.
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.
Your version of jq evidently requires the .
filter here, as in:
jq -s -R .
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With