Iterate over json array of dates in bash (has whitespace)

bash iterate json array
jq iterate over array
jq loop through object
jq foreach
bash array
bash for loop jq array
bash array to json jq
jq foreach example

I have an array that is being stored in a bash variable that is a json array of dates (it's currently being produced by a separate script). It looks like the following

["2019-09-19 03:13:29", "2019-09-19 20:20:18", "2019-09-19 18:19:50", "2019-09-19 06:07:17", "2019-09-19 11:53:25"]

I want to iterate over these dates and preserve the white space in the dates so I can use them with the gdate command. I'm currently using jq but that is splitting up the date part from the time part. i.e.

dates=$(python | jq -c -r '.[]')
for date in ${dates[@]}; do
    echo $date
    #end goal is to do something here w/ gdate ex gdate -d $date ...

However this gives something like


Where I'm looking for something like the following

2019-09-19 03:13:29
2019-09-19 20:20:18
2019-09-19 18:19:50
2019-09-19 06:07:17
2019-09-19 11:53:25

(one confusing thing for me here is that if I just do the python | jq -c -r '.[]' command in a terminal it looks the way I want it to)

However ideally I would want something like. Is there anyway to get this result from the input in shell script

Your jq is fine; the problem is in the way you're looping through the bash array, as can be seen from this bash typescript:

$ ary=("a b" "c d")
$ for x in ${ary[@]} ; do echo "$x" ; done
$ for x in "${ary[@]}" ; do echo "$x" ; done
a b
c d

Bash for Loop Over JSON Array Using JQ, With JQ and a Bash for loop you can read a JSON config file from Bash and you just want to read a JSON config file from Bash and iterate over an array. with a Bash for loop if our data does not contain spaces or newlines. How to iterate through an array of strings, and remove parts of their values? (BASH) I have an array populated with a list of sites saved as strings, and I want to grab the sections of them that are to the left of '' and save them to a new array.

Guide to Linux jq Command for JSON Processing, This means that working with JSON via the command line can be An important point to note is that if one of the properties has spaces or We'll start with a really basic example to demonstrate how to iterate over an array: ? You should use json_pp tool (in debian, it is part of the libjson-pp-perl package) One would use it like this : cat file.json | json_pp. And get a pretty print for your json. So in your case, you could do :

looping through JSON array in shell script, For the use case provided in the Question, @JigglyNaga's answer is probably better than this, but for some more complicated task, you could also loop through � The Bash provides one-dimensional array variables. Any variable may be used as an array; the declare builtin will explicitly declare an array. There is no maximum limit on the size of an array, nor any requirement that members be indexed or assigned contiguously. Arrays are indexed using integers and are zero-based.

JSON array to bash variables using jq, Your original version isn't going to be eval able because the author name has spaces in it - it would be interpreted as running a command Doe with the� Arrays in JSON are almost the same as arrays in JavaScript. In JSON, array values must be of type string, number, object, array, boolean or null. In JavaScript, array values can be all of the above, plus any other valid JavaScript expression, including functions, dates, and undefined.

Working with JSON in bash using jq, jq is a powerful tool that lets you read, filter, and write JSON in bash. Note that if a property has a spaces or weird characters in it, you'll have to use quotes. Iteration. Now let's see how iteration works. The array or object value iterator In an array of objects, you can access a property on each item in the� How to Iterate through JSONArray in JavaScript Last Updated on July 16th, 2017 by App Shah 2 comments Sometimes you have to handle some type of array (JSONArray, int Array, etc) at client side, i.e. in jsp or html file.

Introduction to Bash arrays, Enter the weird, wondrous world of Bash arrays. And just as with any other Bash variable, make sure to leave no spaces around the equal sign. In other words, the for loop is looping through all indices $i and reading the $i -th Note here that I'm using the jq tool to parse JSON from the command line. Top Forums Shell Programming and Scripting How to loop through space array variable values to vTemp. written a script like this. #!/bin/bash a=`cat list1` for