Carlos Luis
Carlos Luis

Reputation: 213

Save date to file in json format using bash

I am trying to save the current date in the following format to a file and wrap it in a simple json body, so far I was thinking of something like this:

jsonTimeStamp=`$({"timestamp":"$(date +%FT%T.%3N%Z)"})`; > deps/image-details.json 

zsh: command not found: timestamp:2020-10-16T10:36:49.3NEDT

this gives me the following error:

command not found: timestamp:2020-10-16T10:38:38.3NEDT

I am out of ideas, can someone please point to what's going wrong here?

Upvotes: 0

Views: 983

Answers (3)

glenn jackman
glenn jackman

Reputation: 247062

You could let date itself output the JSON:

date '+{"timestamp":"%FT%T.%3N%Z"}'
{"timestamp":"2020-10-16T15:00:11.214EDT"}

Have a close look at what you're doing in your question. I'm going to replace the semicolon with a newline:

jsonTimeStamp=`$({"timestamp":"$(date +%FT%T.%3N%Z)"})`
> deps/image-details.json 

You set the variable on the first line, and on the second line you truncate the output file to zero bytes.

Upvotes: 1

Léa Gris
Léa Gris

Reputation: 19655

jq -cn '{"timestamp":(now|todateiso8601)}' >deps/image-details.json

or

jq -cn '{"timestamp":(now|todate)}' >deps/image-details.json

or

jq -cn '{"timestamp":(now|strftime("%FT%T"))}' >deps/image-details.json

Upvotes: 1

Frank Neblung
Frank Neblung

Reputation: 3175

echo "{\"timestamp\": \"$(date +%FT%T)\"}" > my.json

Upvotes: 2

Related Questions