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
-Rreads the input as "raw" (i.e. unquoted strings) and-nhands over stdin toinputs(slurping with-sdoesn't work because when combined with-R, it reads the whole input a single string). Add-cto 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
\nas well for solving the above case, but my (unverified) assumption is that the above solution is more portable with systems using other line terminators.