I have a JSON file which is an array of Object. I want to parse the file and read the data from it using shell scripting.
file.json:-
[
{
"serviceName": "CreateAssociationService",
"targetJarFile": "../../create-association-service/target/create-association-service-1.0.0-aws.jar",
"function_name": "CreateAssociationService",
"handler": "org.qmetech.aws.handler.CreateAssociationFunctionHandler",
"method": "POST"
},
{
"serviceName": "CreateAssociationService1",
"targetJarFile": "../../create-association-service/target/create-association1-service-1.0.0-aws.jar",
"function_name": "CreateAssociationService",
"handler": "org.qmetech.aws.handler.DataHandler",
"method": "GET"
}
]
Does anyone know how can I do it?
As suggested :-
I am using the below command to read the file.
config_file="./file.json"
length=`jq '. | length' $config_file`
i=1
while (( $i <= $length ))
do
serviceName=`cat $config_file | jq '.[$i].serviceName'`
targetJarFile=`cat $config_file | jq '.[$i].targetJarFile'`
function_name=`cat $config_file | jq '.[$i].targetJarFile'`
handler= `cat $config_file | jq '.[$i].handler'`
method= `cat $config_file | jq '.[$i].method'`
echo "-service Name" "$serviceName"
echo "-targetJarFile" "$targetJarFile"
i++
done
But this code goes in an infinite loop with the below error:-
./deploy.sh: line 34: i++: command not found
jq: error: $i is not defined at <top-level>, line 1:
.[$i].serviceName
jq: 1 compile error
jq: error: $i is not defined at <top-level>, line 1:
.[$i].targetJarFile
jq: 1 compile error
jq: error: $i is not defined at <top-level>, line 1:
.[$i].targetJarFile
jq: 1 compile error
and the value of $i is also not evaluated properly.