Reputation: 21
Below is the json response of an api. Need to fetch exchange rate from the json using jq(within shell script). Below are the conditions: startdate should be eqaul to or greater than current date and enddate should be equal to or less than the current date for given currencyfrom and currencyto.
[
{
"status": "ACTIVE",
"startdate": "2019-01-31T00:00:00.000Z",
"enddate": "2019-02-07T00:00:00.000Z",
"source": "default",
"exchangerate": "12",
"currencyfrom": "AUD",
"currencyto": "BRL",
"id": "64ce2916-af8a-42b9-9fb9-def47f824ea2"
},
{
"status": "ACTIVE",
"startdate": "2019-03-10T00:00:00.000Z",
"enddate": "2019-03-30T00:00:00.000Z",
"source": "default",
"exchangerate": "13",
"currencyfrom": "BRL",
"currencyto": "GBP",
"id": "4fd0dc10-d6b7-4298-924d-281a0d49c2e9"
},
{
"status": "ACTIVE",
"startdate": "2019-03-10T00:00:00.000Z",
"enddate": "2019-03-10T16:20:10.813Z",
"source": "default",
"exchangerate": "17",
"currencyfrom": "AUD",
"currencyto": "BRL",
"id": "52bfe481-f4cf-4822-9566-886c4faeaf10"
}
]
Upvotes: 1
Views: 60
Reputation: 116770
Ignoring timezone considerations, you could use the following helper function as the basic building block for time comparisons:
def secs: sub("\\....(?<z>.)$"; .z) | fromdate;
With it, you could select the JSON objects within the array, while preserving the array structure, as follows:
now as $now
| map(select(.startdate|secs) <= $now and $now <= (.enddate|secs)) )
This doesn't quite match your description, but it does make sense.
Once the objects have been selected, you can extract the fields of interest in the usual way.
Upvotes: 1