Having a list of strings (one per line) like
str1
str2
...
how do I convert these into the JSON list ["str1", "str2", ...]
?
Having a list of strings (one per line) like
str1
str2
...
how do I convert these into the JSON list ["str1", "str2", ...]
?
Assuming that the input is given on stdin, the following command solves the problem:
The flag
-R
reads the input as "raw" (i.e. unquoted strings) and-n
hands over stdin toinputs
(slurping with-s
doesn't work because when combined with-R
, it reads the whole input a single string). Add-c
to print the JSON on one line like in the question.Any empty lines (like a trailing newline) may be skipped by adding a little filter:
If the strings are separated by other characters like
,
, the string may be split withThis could be used to split on
\n
as well for solving the above case, but my (unverified) assumption is that the above solution is more portable with systems using other line terminators.