Powershell-读取目录中的多个json文件并输出数据

时间:2019-11-27 21:57:02

标签: json powershell

我必须启动一个进程,该进程将读取另一个进程创建的多个json文件。

我有可以读取单个文件的代码,但是我们需要批量处理这些结果。

这是我当前的代码:

$json = Get-ChildItem $filePath -recurse | Where-Object { $_.LastWriteTime -gt [DateTime] $filesNewerThan } | ConvertFrom-Json

$json.delegates | foreach-Object {
    foreach ($File in $_.files) 
    {
        [PSCustomObject]@{
            LastName = $_.lastName
            ZipCode = $File.zipCode
            BirthDate = $File.birthdate
            Address = $File.Address}
    }
  }

现在,我收到有关“无效JSON原语”的错误,我猜这是我的代码中未指定“ Get-Content”的问题。

想知道我的代码有什么问题。

1 个答案:

答案 0 :(得分:0)

ConvertFrom-Json当前(自PowerShell 7.0起)不支持file- path 输入,仅支持file- content 输入(实际JSON字符串),这意味着您需要参与Get-Content

$json = Get-ChildItem -File $filePath -Recurse | 
  Where-Object { $_.LastWriteTime -gt [DateTime] $filesNewerThan } |
    ForEach-Object { Get-Content -Raw -LiteralPath $_.FullName | ConvertFrom-Json }
相关问题