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".

4
  • A quick workaround is to use eval; like eval array=\($(echo ... | jq ...)\), but this is considered a bad practice Commented Feb 4, 2020 at 7: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. Commented Feb 4, 2020 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? Commented Feb 4, 2020 at 10:04
  • Inian, your comment about mapfile was really helpful, but you removed it? Commented Feb 7, 2020 at 15:42

1 Answer 1

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 $ 
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.