Sam G
Sam G

Reputation: 161

Adding today's date into a CSV while converting a JSON to CSV using JQ

I have a JSON result from ElasticSearch that I parse with jq and retrieve the values I need out of the JSON and flatten it to CSV. There is no date in the fields coming from the JSON file. I need to be able to write a specific date as the first value in each row of the CSV.

ES_Query | jq -r '.aggregations.distinct_UUID.buckets[] | (.latest.hits.hits[]._source | [."_uuid",."site_name",."Jar"]) + (.PS_percentiles.values | [."80.0",."95.0"]) | @csv' >> /home/Outputs/res_wk_${end_date[$weeknum]}.csv

For example, in each row, I want to write the output of date +%F before the UUID, Sitename and Jar values. Is it possible?

B T W, I tried adding (date +%F) at the beginning and ending of the jq and got a compile time error.

Upvotes: 0

Views: 431

Answers (1)

peak
peak

Reputation: 116880

There are basically two ways to proceed:

1) If your jq is sufficiently recent, use jq's time-and-date functions, starting with now:

$ jq -n now
1493069762.538462

$ jq -n 'now|strftime("%Y-%m-%d")'
"2017-04-24"

For further details, see the "Date" section of the online manual: https://stedolan.github.io/jq/manual/

2) Pass the date string in to jq using a suitable command-line option, e.g.

$ jq -n --arg date "$(date +%F)" '$date'
"2017-04-24"

Upvotes: 2

Related Questions