Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to append current date and time into existing json file using jq

Tags:

json

date

jq

I have below format of json file

{
  "username":"achu",
  "password":"test1234"
}

I just want to add timestamp into the above payload and send it as request for some service.

AS per I know the below command will help us to get current stamp on linux:

date +"%r"

but not sure how can I append this into the above payload as like below:

Expected:

{
  "username":"achu",
  "password":"test1234",
  "date":"1:20:30 AM PST"
}

jq --version

1.5v

Is it possible to get it like this?

like image 269
ArrchanaMohan Avatar asked Sep 15 '25 23:09

ArrchanaMohan


2 Answers

JQ has builtin functions for playing with dates, such as:

. + {date: (now | strflocaltime("%r"))}
like image 197
oguz ismail Avatar answered Sep 19 '25 16:09

oguz ismail


One of many possibilities:

$ jq --arg date $(date +"%r") '. + {date: $date}'

p.s. The jq program could be written as:

. + {$date}

p.p.s. Rather than continually asking whether something is possible using jq, you might find it a better investment of your time to become more familiar with jq. A good reference is the official manual: https://stedolan.github.io/jq/manual/

like image 41
peak Avatar answered Sep 19 '25 14:09

peak