I have a requirement to read some fields of json data from a file to compose a new json list. The file is generated by reading data from multiple servers via pssh
# pssh_result.txt
[1] 14:44:51 [SUCCESS] 1.1.1.1
{"field1":"value11","field2":"value12","field3":"value13",...}
[2] 14:44:51 [SUCCESS] 1.1.1.2
{"field1":"value21","field2":"value22","field3":"value23",...}
...
...
I can only extract fields from it at the moment, but I don't know how to combine them into a finished json array
#!/bin/sh
input="pssh_result.txt"
while IFS= read -r line; do
# echo "$line"
if jq -e . >/dev/null 2>&1 <<<"$line"; then
read field1 field3 <<<$(echo $(echo $line |
jq -r '.field1, .field3'))
example_item=$(jq --null-input \
--arg field1 "$field1" \
--arg field3 "$field3" \
'{"alias1": $field1, "alias2": $field3}')
echo $example_item
fi
done <"$input"
the output are as follow
sh test.sh
{ "alias1": "value11", "alias2": "value13" }
{ "alias1": "value21", "alias2": "value23" }
But I want to merge it into a large json array for later sending to a third party for processing, and the desired result is as follows
[
{
"alias1": "value11",
"alias2": "value13"
},
{
"alias1": "value21",
"alias2": "value23"
}
]
I also thought about putting it into a array and then passing it through something like join(arr,","), but it didn't work as expected.
How do I get the results I want? I really appreciate any help with this.