Reading Raw Text into jq
How do you read non-JSON inputs with jq?
This isn’t obvious if you don’t already know how to do it…
The manual says:
Clear yet?
An Example
The lines in fruits.txt
are “naked” strings; not JSON strings (missing double
quotes). What --raw-input
does is wrap your lines in double quotes: notice
the dangling spaces after cherry
.
If you try to load fruits.txt
without --raw-input
, you get:
Once “bootstrapped” into jq, you can treat it as any JSON input:
Slurp
Technically, fruits.txt
contains 6 lines: it becomes 6 JSON items, treated separately.
If you’ve done enough jq, your solution is to --slurp
it, to get an array of strings:
Oh no … 😬
As per the --raw-input
documentation (helpfully at the top of this
post), --slurp
works surprisingly for this case… The whole input, all the
lines, is considered as one giant string.
What’s the workaround? As far as I could tell, you are going to need two jq commands to make this work:
Discussion
jq is wonderful once you have JSON … but a lot of command-line tools don’t produce JSON natively.
Using this technique, you can uplift raw text into JSON and modify it into the shape you need. All the while, you are guaranteed to keep and generate valid JSON outputs.
The alternatives of text manipulation with sed/awk/perl/etc are (at best!) too error-prone.