简体   繁体   中英

Create JSON file from multiple txt files

I have the following problem:

*I want a JSON files with the keys, {"key1","key2"} each having several values and are written in a txt file. The txt file for each key looks like this:

key1.txt:
val1
val2
val3
..

and similar structure for key2.txt. Using these I want to create a json file that has the following structure:

{
    key1:
    [ "val1"
      "val2"
       ...
    ]

    key2:
    [ "val1"
      "val2"
       ...
    ]
}

I hope the question is clear. I prefer using bash to do this, if possible.

Try below one

#!/bin/bash
json="{"
for f in <path of directory>/*.txt
do
filename=$(basename "$f")
filename="${filename%.*}"  
json=$json'"'$filename'"'": ["
while IFS= read -r line; do
echo $filename
json=$json'"'$line'",'
done < $f  
json="${json%?}"
json=$json"],"
done
json="${json%?}"
json=$json"}"
echo $json

jq can be used to read in multiple files using the --slurpfile option. The files however must be an actual json file. Since your files aren't in a json format, you need to convert them.

$ jq -R '.' key1.txt > /tmp/key1.json && \
  jq -R '.' key2.txt > /tmp/key2.json && \
  jq --slurpfile key1 /tmp/key1.json \
     --slurpfile key2 /tmp/key2.json \
     -n '{ key1: $key1, key2: $key2 }'

Otherwise, if you don't want to deal with temporary files, you could use a subshell to create the json arrays and pass them in using --argjson .

$ jq --argjson key1 "$(jq -R '.' key1.txt | jq -s '.')" \
     --argjson key2 "$(jq -R '.' key2.txt | jq -s '.')" \
     -n '{ key1: $key1, key2: $key2 }'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM