Reading Files in JSON

I want to read in several files (index.html, style.css, main.js) to create a JSON payload to load.

I know with nodejs, I can start creating what I want, like this:

var fs = require('fs');
fs.readFile('index.html', 'utf8', function (err, data) {
  if (err) throw err;
  out = JSON.stringify({"html": data});
  console.log(out);
});

How do I do this with jq ?

+4
source share
3 answers

One way to process multiple text files is illustrated by the following:

(jq -Rs . a.txt ; jq -sR . b.txt) | jq -s
[
  "1\n2\n",
  "3\n4\n"
]

So, in your case, you would do something like this:

(jq -Rs '{ html: . }' index.html; \
 jq -Rs '{ javascript: . }' main.js; \
 jq -Rs '{ css: . }' style.css) |\
 jq -s add

That is, convert each text file to a JSON string separately, and then pass those lines to jq. This has the advantage that jq 1.5 is not required, but if you have jq 1.5, then a solution using a filter may be preferable inputs.

+1

( jq 1.5):

jq --null-input --raw-input \
  'reduce inputs as $line ({}; .[input_filename] += [$line]) | map_values(join("\n"))' \
  index.html style.css main.js

. :

reduce inputs as $line ({}; .[input_filename] += [$line])
| map_values(join("\n"))

:

$ cat test1.txt
foo
bar
baz

$ cat test2.txt
qux
quux
quuux

$ jq --null-input --raw-input \
  'reduce inputs as $line ({}; .[input_filename] += [$line]) | map_values(join("\n"))' \
  test1.txt test2.txt
{
  "test1.txt": "foo\nbar\nbaz",
  "test2.txt": "qux\nquux\nquuux"
}

P.S. , :

reduce inputs as $line ({}; .[input_filename] += "\($line)\n")
+2

Use the raw ( -R) command-line option to read input as a string. Then you can create your json result. You will also want to do this ( -s) for multi-line text files.

$ jq -Rs '{ html: . }' index.html

However, this only works for text files. If you have binaries, you must first encode them. You can use base64for this.

$ base64 -w0 image.jpg | jq -R '{ jpg: . }'
0
source

Source: https://habr.com/ru/post/1613283/


All Articles