0

I try to convert json files to html files. So I can later import them to the website. My idea is run json2table to create a table and write then to the html directory. But I find not out how can redirecting the output of json2table.

for file in $(ls json/*.json); do cat $file | json2table > html/$file.html; done

But this end with

bash: html/json/26.json.html: No such file or directory

Is there someone which can help to fix the error? Thank you

Silvio

Silvio
  • 123
  • 1
  • 9

3 Answers3

2

Your ${file} includes the json directory. When you don't have a directory html/json the redirection will fail.
I think you have a subdir html and stripping the path from ${file} will work:

for file in json/*.json; do 
   cat "${file}" | json2table > html/"${file#*/}".html
   # OR avoid cat with
   # json2table < "${file}" > html/"${file#*/}".html
done

As a bonus I removed the ls, added quotes for filenames with spaces and showed in a comment how to avoid cat.

Walter A
  • 19,067
  • 2
  • 23
  • 43
  • I suggest to replace `${file%/*}` with `${file#*/}`. – Cyrus Jul 01 '18 at 20:39
  • @Cyrus Good idea, tx. – Walter A Jul 01 '18 at 21:25
  • +1 for removing `ls` but I think he needs to use something like `"$(basename $file)"` instead of `${file#*/}` to get the filename only. Otherwise that's will try to create some files with `html/json/{filename}.html` insteaf of `html/{filename}.html` as path. – Idriss Neumann Jul 01 '18 at 21:43
  • `file=json/example.json; echo "${file#*/}" ` will return `example.json`. The `file` returned by `json/*.json` will have one slash, never two. You might want to replace all `json` with `cat "${file} | json2table > "${file//json/html}"`, but this will fail for `json/json_in_basename.json`. notpossible/example.json – Walter A Jul 01 '18 at 22:05
  • Indeed, I tried with a subdir `json` and it works with this loop. I think it remain a lot more safer to use `basename` (which is a builtin in bash) to get the filename only. Anyway, I've given +1 to this answer :p – Idriss Neumann Jul 02 '18 at 07:18
  • Thank you this is the solution. Thank you and thx for the ls information. This was new. Thanks. – Silvio Jul 02 '18 at 08:04
0

I want give all the complete script if people have same problem / project or how will call it.

The first loop generates an HTML table from the JSON files and stores it in an HTML file. The second loop combines the result of the first loop with a template, so that you have a complete page. In the body tag, the include is simply searched for, deleted and filled with the table.

#!/bin/bash

jdata="json"
hdata="html" 
template="tpl/tpl.html"
tmp="tmp"

# convert json to html
# https://stackoverflow.com/questions/51126226/combine-for-loop-with-cat-and-json2html/51126732#51126732

for file in $jdata/*.json;
do
    # php
    #php json.php $file > tmp/${file#*/}.html

    # ruby
    json2table < "${file}" > $hdata/"${file#*/}".html
done

# write html file 
# https://unix.stackexchange.com/questions/32908/how-to-insert-the-content-of-a-file-into-another-file-before-a-pattern-marker

for file in html/*.html;
do
    # extract Project Number for newfile 
    filename="$(basename -- "$file")"
    extension="${filename#*.}"
    filename="${filename%.*}"

    # save the project number
    NO="$(grep "No" $jdata/$filename | sed 's/[^0-9]*//g')"
    OUT="$NO.html"

    # write new html file
    sed 's/include/$x/g' tpl/tpl.html | x="$(<$file)" envsubst '$x' > html/$OUT

done

Thank you for help & Nice day Silvio

Silvio
  • 123
  • 1
  • 9
-1

Your ls command returns the directory as well as the file name.

Ex. json/1.json json/2.json json/3.json

So do:

for file in $(/bin/ls json/*.json)
do
    cat $file | json2table >html/$(basename $file).html
done
  • I always use the full path for ls in such conditions since I want to make sure I do not use any alias that might have been defined on it.
  • basename removes the directory out of $file
Nic3500
  • 8,144
  • 10
  • 29
  • 40