I used Centos7 and jq-1.6. I'm hoping to convert every UTC TimeStamp in the json to Singapore time(SNST=GMT 08:00) I'm currently writing shellscript so I can mix some bash commands if needed.
The json source
api_result='
{
"DataInterval": 300,
"EndTime": "2022-07-25T15:15:00Z",
"HttpCodeData": {
"UsageData": [
{
"TimeStamp": "2022-07-29T03:00:00Z",
"Value": {
"CodeProportionData": [
{
"Code": "404",
"Count": 36,
"Proportion": 0.022
},
{
"Code": "502",
"Count": 3,
"Proportion": 0.002
}
]
}
},
{
"TimeStamp": "2022-07-29T03:05:00Z",
"Value": {
"CodeProportionData": [
{
"Code": "404",
"Count": 27,
"Proportion": 0.015
},
{
"Code": "502",
"Count": 5,
"Proportion": 0.002
}
]
}
}
},
"StartTime": "2022-07-25T15:10:00Z"
}'
My attempt I first tried to convert the timestamp to epoch. But it failed
jq '
def datestamp2epoch:
. | scan("(. ?)([.][0-9] )?Z$")
| [(.[0] "Z" | fromdateiso8601), (.[1] // 0 | tonumber)]
| add;
map(.HttpCodeData.UsageData[].TimeStamp |= datestamp2epoch)
' <<< ${api_result}
ERROR
jq: error (at <stdin>:1): Cannot index number with string "HttpCodeData"
CodePudding user response:
Drop map
as your document is an object, not an array. Then, to convert the timestamps to Unix epoch seconds, it suffices to apply fromdateiso8601
:
jq '.HttpCodeData.UsageData[].TimeStamp |= fromdateiso8601' <<< "${api_result}"
{
"DataInterval": 300,
"EndTime": "2022-07-25T15:15:00Z",
"HttpCodeData": {
"UsageData": [
{
"TimeStamp": 1659063600,
"Value": {
"CodeProportionData": [
{
"Code": "404",
"Count": 36,
"Proportion": 0.022
},
{
"Code": "502",
"Count": 3,
"Proportion": 0.002
}
]
}
},
{
"TimeStamp": 1659063900,
"Value": {
"CodeProportionData": [
{
"Code": "404",
"Count": 27,
"Proportion": 0.015
},
{
"Code": "502",
"Count": 5,
"Proportion": 0.002
}
]
}
}
]
},
"StartTime": "2022-07-25T15:10:00Z"
}
Note: If you wanted to include also .StartTime
and .EndTime
, just list them along on the LHS:
(.StartTime, .HttpCodeData.UsageData[].TimeStamp, .EndTime) |= fromdateiso8601