Home > database >  Convert mutliline shell variable to JSON string
Convert mutliline shell variable to JSON string

Time:09-26

How can I convert a multiline variable with special characters into a JSON string using JQ?

$ echo $FPR_LOW
4 issues of 4 matched search query.

Issue counts by category:

 J2EE Bad Practices: Leftover Debug Code => 1 Issues
     ApiApplication.java:23 (Structural)
 Poor Error Handling: Overly Broad Throws => 3 Issues
     controller/BanksController.java:27 (Structural)
     controller/BanksController.java:40 (Structural)
     controller/BanksController.java:51 (Structural)

The above string variable should be a single JSON string.

CodePudding user response:

Invoke jq with raw-input and slurp flags.

printf '%s' "$FPR_LOW" | jq -Rs .

CodePudding user response:

Or pass the value as a jq variable (and tell it not to expect input):

jq -n --arg fpr "$FPR_LOW" '$fpr'
  • Related