JQ: Transform UNIX Timestamp to Datetime
Asked Answered
T

2

17

I use actually JQ1.5 under a Windows 10 environment to transform several json files for an import to a MS SQL database. Part of the data are formatted in UNIX timestamp and I need to transform those data to ISO 8601 Format.

Following command i use actually for the transformation of the data:

jq '
[
  { nid, title, nights, zone: .zones[0].title} + 
  (.sails[] | { sails_nid: .nid, arrival, departure } ) + 
  (.sails[].cabins[] | 
    { cabintype: .cabinType.kindName, 
      cabinid:   .cabinType.nid,  
      catalogPrice, 
      discountPrice, 
      discountPercentage, 
      currency 
    }
  )
]
' C:\Import\dreamlines_details.json > C:\Import\import_sails.json

Arrival and departure are the data that are in Unix time formated.

Data:

[
  {
    "nid": 434508,
    "title": "Die schönsten Orte unserer Welt",
    "nights": 121,
    "zone": "Weltreise",
    "sails_nid": 434516,
    "arrival": 1525644000,
    "departure": 1515193200,
    "cabintype": "Innenkabine",
    "cabinid": 379723,
    "catalogPrice": 17879,
    "discountPrice": 9519,
    "discountPercentage": 0.4675876726886291,
    "currency": "EUR"
  },
  {
    "nid": 434508,
    "title": "Die schönsten Orte unserer Welt",
    "nights": 121,
    "zone": "Weltreise",
    "sails_nid": 434516,
    "arrival": 1525644000,
    "departure": 1515193200,
    "cabintype": "Innenkabine",
    "cabinid": 379730,
    "catalogPrice": 18599,
    "discountPrice": 10239,
    "discountPercentage": 0.44948653153395346,
    "currency": "EUR"
  }
]

I experimented with built in operator "todate" and "strftime". But get only parsing Errors.

Tham answered 12/6, 2017 at 10:47 Comment(3)
you should post the input json fragment with crucial dataUnstressed
Hi @Unstressed added the dataTham
Your code and example json do not match. Please consider simplifying your code and example adhering to the MCVESpringe
U
26

Use todateiso8601 function:

jq '.[].arrival |= todateiso8601 | .[].departure |= todateiso8601' C:\Import\import_sails.json

The output (for your input fragment):

[
  {
    "nid": 434508,
    "title": "Die schönsten Orte unserer Welt",
    "nights": 121,
    "zone": "Weltreise",
    "sails_nid": 434516,
    "arrival": "2018-05-06T22:00:00Z",
    "departure": "2018-01-05T23:00:00Z",
    "cabintype": "Innenkabine",
    "cabinid": 379723,
    "catalogPrice": 17879,
    "discountPrice": 9519,
    "discountPercentage": 0.4675876726886291,
    "currency": "EUR"
  },
  {
    "nid": 434508,
    "title": "Die schönsten Orte unserer Welt",
    "nights": 121,
    "zone": "Weltreise",
    "sails_nid": 434516,
    "arrival": "2018-05-06T22:00:00Z",
    "departure": "2018-01-05T23:00:00Z",
    "cabintype": "Innenkabine",
    "cabinid": 379730,
    "catalogPrice": 18599,
    "discountPrice": 10239,
    "discountPercentage": 0.44948653153395346,
    "currency": "EUR"
  }
]
Unstressed answered 12/6, 2017 at 11:28 Comment(3)
For anyone looking for the inverse, it is fromdateiso8601Iams
For anyone needing to do a similar translation but from time in milliseconds, the filter (fragment after |=) can be like this: (. / 1000 | round | todateiso8601).Arnaud
A shorter version applies the function to both fields: jq '.[]|(.arrival,.departure) |= todateiso8601' C:\Import\import_sails.json. An example is found in the docs for the update-assignment operator.Hartz
C
1

I had a similar problem when parsing output of Perforce (with -Mj option), but the epoch times were strings, not numbers.

$ p4 -z tag -Mj labels -e "test_build" | jq '.'
{
  "Access": "1581356898",
  "Description": "Created by p4build.\n",
  "Options": "unlocked noautoreload",
  "Owner": "p4build",
  "Update": "1580936739",
  "label": "test_build"
}

Adding tonumber into the filter fixes it:

$ p4 -z tag -Mj labels -e "test_build" > test.json
$ jq -s '.[].Access |= (tonumber | todateiso8601) | .[].Update |= (tonumber | todateiso8601)' test.json
[
  {
    "Access": "2020-02-10T17:48:18Z",
    "Description": "Created by p4build.\n",
    "Options": "unlocked noautoreload",
    "Owner": "p4build",
    "Update": "2020-02-05T21:05:39Z",
    "label": "test_build"
  }
]
Cultivar answered 7/5, 2021 at 0:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.