0

I have a problem to assign value to bash array correctly which was parsed by jq. I have a JSON output from curl:

{
  "id": 6442,
  "name": "Execute Workflow",
  "description": "Plan: data",
  "status": "In Queue",
  "start_date": 0,
  "end_date": 0,
  "job_type": "Execute Workflow",
  "created_by_name": null,
  "creation_date": 1580762385615,
  "creation_date_str": "02/03/2020 09:39:45 PM",
  "last_updated_date": 1580762385615,
  "last_updated_date_str": "02/03/2020 09:39:45 PM",
  "last_updated_by_name": null,
  "schedule_on": 0,
  "paused_at_step": 0,
  "percent_complete": 0,
  "job_action_type": null,
  "child_job_id": -1
}

I want to save two key values .id and .status into bash array.

I am doing it this way:

array=( $(echo '{ "id": 6442, "name": "Execute Workflow", "description": "Plan: data", "status": "In Queue", "start_date": 0, "end_date": 0, "job_type": "Execute Workflow", "created_by_name": null, "creation_date": 1580762385615, "creation_date_str": "02/03/2020 09:39:45 PM", "last_updated_date": 1580762385615, "last_updated_date_str": "02/03/2020 09:39:45 PM", "last_updated_by_name": null, "schedule_on": 0, "paused_at_step": 0, "percent_complete": 0, "job_action_type": null, "child_job_id": -1}' | jq '.id, .status') ) 

All seems OK until I try to get second element of that array: echo ${array[1]} and I get "In not "In Queue".

My array is 3 elements long echo ${#array[@]} returns 3 but I want it to be 2 elements long. Can someone help me please?

My next steps in bash script is to assign job_status="=${array[1]}" and I want to get variable job_status="In Queue".

sergei
  • 402
  • 1
  • 5
  • 14
  • A quick workaround is to use `eval`; like `eval array=\($(echo ... | jq ...)\)`, but this is considered a bad practice – oguz ismail Feb 04 '20 at 07:16
  • OP's comment in my now deleted answer which did not make sense to me - [1 of 2] The background.. probably you give me an advice what I am doing wrong. This curl command executes the job on background and returns that json what I have posted before. Then I want to check whether job succeeded. I have another curl command to check it through REST API. I save jobId and status into array and assign 1-st element of it to job_status var and then check it with curl in until loop. – Inian Feb 04 '20 at 10:04
  • [2 of 2] In the beginning the status is "In Queue", then it changes to "Running" and then "Error" or "Succeeded". Theese outputs I will save into variable not into array, seems OK? – Inian Feb 04 '20 at 10:04
  • Inian, your comment about mapfile was really helpful, but you removed it? – sergei Feb 07 '20 at 15:42

1 Answers1

1

yes, when you assign to an array, bash has to escape all its special characters and then split the arguments with the default separator, which is space. There's no matter of quoting of arguments or spaces per se in the source JSON that would help here.

Thus, to work-around that you'd need to set IFS to the delimiter which is unique enough for your JSON data - for the sake of the example, let it be comma (assuming your JSON is in curl.json for brevity):

bash $ ifs="$IFS"; IFS=','; array=($(<curl.json jq -r '[.id, .status] | @csv')); IFS="$ifs"
bash $ echo ${#array[@]}
2
bash $ echo ${array[1]}
"In Queue"
bash $ 
Dmitry
  • 1,275
  • 1
  • 5
  • 14