【问题标题】:Parsing and manipulating Dates in a Json file using PowerShell使用 PowerShell 解析和操作 Json 文件中的日期
【发布时间】:2021-07-06 21:10:58
【问题描述】:

我需要帮助解析 JSON 文件中的日期。

简而言之,我的 PowerShell 脚本将信息从 JSON 源文件提取到 CSV。我的 Json 文件有这些字段:

[
    {
     "Type":  "Some String",
     "Score": "Some String" ,
     "Dateobtained": "2021-07-03T07:15:48.493",
     "Status": "Some String", 
     "Description": "Some String" 
    },
    { 
     "Type":  "Some String",
     "Score": "Some String" ,
     "Dateobtained": "2021-06-24T07:15:48.493",
     "Status": "Some String", 
     "Description": "Some String"
    }
]

我想过滤掉旧信息。例如,今天是 7 月 6 日,我只想包含仅在今年 7 月获得的信息(Dateobtained 不应提取 7 月之前的数据)

我已经有了这个来提取其他信息:

$json = Get-Content "source.json" | ConvertFrom-Json

$json | ForEach-Object {
# Iterate through each of the tests and create object for each
    $_.Findings| ForEach-Object {
        [PSCustomObject]@{
            'Type'       = $_.Type
            'Score'     = $_.Score
            'Status' = $_.Status
            'Description' = $_.Description
            }
    }
    
} | Export-Csv -Path "out.csv" -NoTypeInformation

我不太确定如何继续解析该日期,然后将 CSV 文件中输出的内容限制为当前月份。

提前谢谢你:-)

【问题讨论】:

    标签: json powershell date timestamp


    【解决方案1】:

    给定以下数组:

    PS /> $json | ft
    
    Type        Score       Dateobtained            Status      Description
    ----        -----       ------------            ------      -----------
    Some String Some String 2021-07-03T07:15:48.493 Some String Some String
    Some String Some String 2021-06-24T07:15:48.493 Some String Some String
    Some String Some String 2021-05-24T07:15:48.493 Some String Some String
    Some String Some String 2021-04-24T07:15:48.493 Some String Some String
    

    如果您只想过滤DateObtained 在当前月份之间的那些对象(对不起我的英语),您可以这样做:

    $thisMonth = [datetime]::Today.Month
    
    $json | Where-Object {
    
        ([datetime]$_.dateobtained).Month -eq $thisMonth
        # OR
        # ($_.dateobtained -as [datetime]).Month -eq $thisMonth
    
    } |
    Select-Object * -ExcludeProperty Dateobtained |
    Export-Csv -Path "out.csv" -NoTypeInformation
    

    这只会产生一个对象(第一个):

    Type        Score       Status      Description
    ----        -----       ------      -----------
    Some String Some String Some String Some String
    

    注意:

    如果所述属性没有实际日期,则在$_.Dateobtained 上键入[datetime] 可能会引发错误。如果您不确定,请改用 $_.Dateobtained -as [datetime]

    PS /> [datetime]'notadate'
    Cannot convert value "notadate" to type "System.DateTime". Error: "The string was not recognized as a valid DateTime. There is an unknown word starting at index 0."
    At line:1 char:1
    + [datetime]'notadate'
    + ~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : InvalidArgument: (:) [], RuntimeException
        + FullyQualifiedErrorId : InvalidCastParseTargetInvocationWithFormatProvider
    
    PS /> 'notadate' -as [datetime] # // No errors here
    
    PS />
    

    【讨论】:

    • 谢谢你。 [datetime]$_.dateobtained).Month 帮助我理解和操作 JSON 文件中的日期。
    • 我很高兴帮助@prokopio_kuba,我希望类型转换[datetime] 与使用-as 的概念也很清楚。如果不让我知道并且我会尝试更好地解释它。
    猜你喜欢
    • 2019-08-20
    • 2018-10-10
    • 2015-07-25
    • 2012-06-02
    • 1970-01-01
    • 2015-03-11
    • 1970-01-01
    • 2012-12-26
    • 1970-01-01
    相关资源
    最近更新 更多