I'm trying to JSON encode all of the contents of a single file into a single JSON string for a curl request in BASH. The file is text so it doesn't need to be base64 encoded. The big thing is that the file is multi-lined and the line breaks matter. I'm trying to encode a PEM file. If I strip the \n out of the file, it breaks the PEM format. I tried to see if jq would help but I don't see any option to tell it to encode newline character.
Anybody have any hints here?
You could use jq with the -s and -R options, e.g.
jq -sR .
a
b
Result:
"a\nb\n"
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