I am currently trying to get information about my file hosting accounts. As I keep a lot of my backup media on different accounts. I am using megatools to query information about the account, which I then parse into an array. The array is then flattened using \n for raw input.
The script works wonderfully but its not creating valid json. I am not sure what I am missing to make it valid. Thanks for the help in advance.
script
function join_by { local IFS="$1"; shift; echo "$*"; }
while IFS='' read -r line || [[ -n "$line" ]]; do
IFS=, read -ra array <<< "$line"
nickname=${array[0]}
user=${array[1]}
pass=${array[2]}
data=($(megadf --username=$user --password=$pass))
data[${#data[@]}]+="$nickname"
stats=$(join_by $'\n' ${data[@]})
echo $stats | jq --slurp --raw-input 'split("\n")[:-1] | map([ split(" ")[] ]) | map({
nick: .[6],
total: .[1],
used: .[3],
free: .[5]
})' >> /opt/stats/json/accounts.json
done < .accounts
json output
[
{
"nick": "alt",
"total": "53687091200",
"used": "7885201595",
"free": "45801889605"
}
]
[
{
"nick": "main",
"total": "214748364800",
"used": "87240914483",
"free": "127507450317"
}
]
What should be
[
{
"nick": "alt",
"total": "53687091200",
"used": "7885201595",
"free": "45801889605"
},
{
"nick": "main",
"total": "214748364800",
"used": "87240914483",
"free": "127507450317"
}
]
.accounts
nickname,user,pass
@barmar found the simplest solution that I overlooked....
Needed to pipe the loop into jq