LoginSignup
2
1

More than 5 years have passed since last update.

JSON内のUNIX時間を人が読めるようにしよう!

Posted at

UNIX時間を脳内で日付に変更できない人向けの内容です。
jqコマンドで私でもパッと見て読めるようにします!

使うもの

サンプルJSON

適当なJSONを用意します。

{"time":[1500000000,1500100000,1500200000,1500300000]}

見やすくする

JSONをコピーしてコマンド実行。
コピー部分はよしなに。

$ pbpaste | jq '.time[] |= strftime("%Y-%m-%dT%H:%M:%SZ")'

出力

{
  "time": [
    "2017-07-14T02:40:00Z",
    "2017-07-15T06:26:40Z",
    "2017-07-16T10:13:20Z",
    "2017-07-17T14:00:00Z"
  ]
}

私でも読めます!!

UNIX時間がstringで入っている場合もあるかもしれない

こんなJSONです。

{"time":["1500000000","1500100000","1500200000","1500300000"]}

コマンドと出力

$ pbpaste | jq '.time[] |= (tonumber | strftime("%Y-%m-%dT%H:%M:%SZ"))'
{
  "time": [
    "2017-07-14T02:40:00Z",
    "2017-07-15T06:26:40Z",
    "2017-07-16T10:13:20Z",
    "2017-07-17T14:00:00Z"
  ]
}

読めます!!!

複数要素にUNIX時間が入っている場合

このようなJSONです。
time[].starttime[].endの2要素があります。

{"time":[{"start":1500000000,"end":1500100000},{"start":1500200000,"end":1500300000}]}

見やすくする

JSONをコピーしてコマンド実行

$ pbpaste | jq '.time[].start |= strftime("%Y-%m-%dT%H:%M:%SZ") | .time[].end |= strftime("%Y-%m-%dT%H:%M:%SZ")'
{
  "time": [
    {
      "start": "2017-07-14T02:40:00Z",
      "end": "2017-07-15T06:26:40Z"
    },
    {
      "start": "2017-07-16T10:13:20Z",
      "end": "2017-07-17T14:00:00Z"
    }
  ]
}

無理やり感あふれるコマンドができあがりました(´・_・`)
詳しい方、いい方法あったら教えてくださいー

2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1