0

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.

Sunny
  • 858
  • 3
  • 17
  • 39
  • 1
    Please add your desired output (no description) for that sample input to your question (no comment). – Cyrus Mar 24 '20 at 19:32
  • Show what you have tried so far. – Cyrus Mar 24 '20 at 19:35
  • You should check it https://stackoverflow.com/questions/38364261/parse-json-to-array-in-a-shell-script/43599684 – chlebek Mar 24 '20 at 20:32
  • Reading the file is easy: `cat file.json`. If you want to parse it somehow, you should describe what information you want to extract. – William Pursell Mar 24 '20 at 21:56
  • Perhaps you want something like: `jq .[].serviceName file.json` – William Pursell Mar 24 '20 at 21:57
  • You have completely changed the question with your edit. The question is now a dup of https://stackoverflow.com/questions/21035121/increment-variable-value-by-1-shell-programming (and probably several hundred other questions). – William Pursell Mar 26 '20 at 12:42

2 Answers2

1

How about using jq tool

cat file.json | jq '.[0]'
{
  "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"
}

cat file.json | jq '.[].serviceName'
"CreateAssociationService"
"CreateAssociationService1"
Chaminda
  • 68
  • 9
0

Given file.json has the values following script I tested and can get the values. Can you check this please. Should work.

count=$(jq -r '. | length' file.json)

for sequence in $(seq $count)
do
  let indx=$sequence-1
  serviceName=$(jq -r ".[$indx].serviceName" file.json)
  targetJarFile=$(jq -r ".[$indx].targetJarFile" file.json)

  # Display variables
  echo $serviceName
  echo $targetJarFile

  echo "---"

done

output

CreateAssociationService
../../create-association-service/target/create-association-service-1.0.0-aws.jar
---
CreateAssociationService1
../../create-association-service/target/create-association1-service-1.0.0-aws.jar
---
Chaminda
  • 68
  • 9