Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiline File in to single JSON string

Tags:

json

bash

jq

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?

like image 669
hydrian Avatar asked Nov 30 '17 16:11

hydrian


1 Answers

You could use jq with the -s and -R options, e.g.

jq -sR .           
a
b

Result:

"a\nb\n"
like image 112
peak Avatar answered Nov 15 '22 03:11

peak