I'm trying to parse the output of a df -i
command into a json template using jq, but I am receiving the following error when reading in numeric values from an array into the json template:
parse error: Invalid numeric literal at line 10, column 31
Here is the script:
#!/bin/sh
JSON=`cat inode-template.json`
#get number of rows in output
numLines=`df -ih | wc -l`
#row iterator (start at 2 to skip the header row)
for ((row=2; row<=$numLines; row++))
do
arrCounter=0 #array counter reset
#for each value in col (minus 'mounted on'):
for ((val=1; val<=5; val++))
do
value=$(df -iP | awk -v value=$val 'NR=='$row'{sub( "%", "", $value); print $value }') #get each value
tempArr[$arrCounter]=$value #store row value
arrCounter=$((arrCounter + 1))
done
echo "${tempArr[@]}"
#Assign values in json template
JSON=$(jq -c --arg filesys "${tempArr[0]}" --arg iTotal ${tempArr[1]} --arg iUsed ${tempArr[2]} --arg iFree ${tempArr[3]} --arg iPercent ${tempArr[4]} '.metrics[].fileSystem |= $filesys | .metrics[].InodesTotal |= $iTotal | .metrics[].InodesUsed |= $iUsed | .metrics[].InodesFree |= $iFree | .metrics[].InodeUsedPercent |= $iPercent' inode-template.json)
echo "${JSON}"
tempArr=() #reset array
done
And here is the json template being read in:
{
"name": "inodeparse",
"metrics": [
{
"event_type": "test",
"provider": "test",
"fileSystem": "FILESYS",
"InodesTotal":NODESTOTAL,
"InodesUsed":NODESUSED,
"InodesFree":NODESFREE,
"InodeUsedPercent":NODESPERCENT
}
]
}
Is there any option to declare an array item as an integer before using jq to replace the value in the template?