2017-06-12 205 views
1

我在Windows 10環境下實際使用JQ1.5將一些導入的json文件轉換爲MS SQL數據庫。部分數據格式爲UNIX timestamp,我需要將這些數據轉換爲ISO 8601格式。JQ:將UNIX時間戳轉換爲日期時間

下面的命令我實際使用的數據的轉換:

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 

抵達和離開是在Unix時間格式化數據。

數據:

[ 
    { 
    "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" 
    } 
] 

我嘗試用內置的運營商 「TODATE」 和 「strftime的」。但只得到解析錯誤。

+0

你應該張貼與關鍵數據輸入JSON片段 – RomanPerekhrest

+0

喜@RomanPerekhrest添加的數據 – TimoC

+1

您的代碼和示例json不匹配。請考慮簡化您的代碼並遵守[MCVE]示例(https://stackoverflow.com/help/mcve) – Thor

回答

1

使用todateiso8601功能:

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

輸出(您的輸入片段):

[ 
    { 
    "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" 
    } 
] 
+0

工程精彩。謝謝! – TimoC

+0

@TimoC,不客氣 – RomanPerekhrest