Search code examples
jsonstringeditjqin-place

How do I replace a value with a substring of that value?


I have some json that looks like this (very simplified):

{"files":[
  {"date":"2018-01-08T11:38:36+00:00"},
  {"date":"2018-01-08T11:38:27+00:00"},
  {"date":"2018-01-07T20:02:12+00:00"},
  {"date":"2018-01-07T18:23:26+00:00"}
]}

I want to convert the dates to epoch seconds so I can sort them. But this format doesn't quite match what strptime will accept:

$ jq '.files[] | .date |= strptime("%Y-%m-%dT%H:%M:%S%z")' files.json 
jq: error (at files.json:6): date "2018-01-08T11:38:36+00:00" does not match format "%Y-%m-%dT%H:%M:%S%z"

So, if I just strip off the last 5 characters I can use the same format, minus the %z. This works:

$ jq '.files[] | .date[0:19]' files.json 
"2018-01-08T11:38:36"
"2018-01-08T11:38:27"
"2018-01-07T20:02:12"
"2018-01-07T18:23:26"

So, what am I doing wrong here:

$ jq '.files[] | map( .date |= .date[0:19] )' files.json 
jq: error (at files.json:6): Cannot index string with string "date"

Thanks to @peak I got it worked out:

$ jq -c '.files[] | .date |= .[0:19]' files.json
{"date":"2018-01-08T11:38:36"}
{"date":"2018-01-08T11:38:27"}
{"date":"2018-01-07T20:02:12"}
{"date":"2018-01-07T18:23:26"}

Solution

  • jq '.files[] | map( .date |= .date[0:19] )' files.json

    There are at least two problems with the above.

    You can either write .date = .date[0:19], or (better): .date |= .[0:19]

    It's not entirely clear what you want, but if the goal is simply to edit the JSON, then one option would be:

    .files |= map( .date |= .[0:19] )