{
"logs": [
{
"timestamp": "20181216T14:36:12",
"description": "IP connectivity via interface ipmp1 has become degraded.",
"type": "alert",
"uuid": "1234567",
"severity": "Minor"
},
{
"timestamp": "20181216T14:38:16",
"description": "Network connectivity via port ibp4 has been established.",
"type": "alert",
"uuid": "12345678",
"severity": "Minor"
}
]
}
I have this JSON object, and I want to iterate through each object and update the timestamp to a more readable date. Right now, I have
$currentLogs.logs |
Where{$_.type -eq 'alert'} |
ForEach{$_.timestamp = {[datetime]::parseexact($_.timestamp, 'yyyyMMdd\THH:mm:ss', $null)}}
But when I read the object $currentLogs, it still hasn't updated.
You will need to first parse your date/time and then apply the formatting you want. If you apply no formatting, then the
timestampproperty will be adatetimeobject type and the conversion back to JSON will do weird formatting to it. It would be best to make your new format a string so that it won't be manipulated by the JSON serialization:In your attempt, you used the following code:
The use of surrounding
{}denotes a script block. If that script block is not called or invoked, it will just output its contents verbatim. You can run the above code in your console and see that result.You also did not format your
datetimeobject after the parse attempt. By default, the output in the console would apply aToString()implicitly when thedatetimevalue is set to a property, but that implicit formatting does not translate to your JSON conversion (for whatever reason).