JSON Powershell 内存问题

JSON Powershell memory issue

我使用命令读取 JSON 文件,一切正常,直到文件变大。

我目前有一个 JSON 文件,大约 1.5GB。我使用 Powershell 使用以下命令读取文件:

get-content -Path C:\TEMP\largefile.json | out-string | ConvertFrom-Json

它returns出现以下错误:

out-string : Exception of type 'System.OutOfMemoryException' was thrown.
+ ... oices = get-content -Path C:\TEMP\largefile.json | out-string | Conve ...
+                                                        ~~~~~~~~~~
+ CategoryInfo          : NotSpecified: (:) [Out-String], OutOfMemoryException
+ FullyQualifiedErrorId : System.OutOfMemoryException,Microsoft.PowerShell.Commands.OutStringCommand

我已经增加了内存,如下所示:

get-item wsman:localhost\Shell\MaxMemoryPerShellMB


WSManConfig: Microsoft.WSMan.Management\WSMan::localhost\Shell

Type            Name                           SourceOfValue   Value
----            ----                           -------------   -----
System.String   MaxMemoryPerShellMB                            8096

关于如何处理这个的任何想法?

编辑(根据评论补充):

当我删除外字符串时出现此错误:

ConvertFrom-Json : Exception of type 'System.OutOfMemoryException' was thrown.
    + ... oices = get-content -Path C:\TEMP\largefile.json | ConvertFrom-Json ...
    +                                                        ~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Out-String], OutOfMemoryException
    + FullyQualifiedErrorId : System.OutOfMemoryException,Microsoft.PowerShell.Commands.OutStringCommand

我的Powershell版本是:5.1.17763.1490

该文件包含有关 PDF 文件的多列。这些文件通过 API 导出到 JSON,因此它包含文件元数据,例如所有者和创建时间,以及 Body 列中的实际 PDF 文件,稍后将被解码为实际文件PDF文件。结构如下:

[{"Id":"ID","ParentId":"parent","Name":"filename","OwnerId":"owner","CreatedDate":"date","Body":"*******"}
{"Id":"ID","ParentId":"parent","Name":"filename","OwnerId":"owner","CreatedDate":"date","Body":"*******"}
{"Id":"ID","ParentId":"parent","Name":"filename","OwnerId":"owner","CreatedDate":"date","Body":"*******"}
{"Id":"ID","ParentId":"parent","Name":"filename","OwnerId":"owner","CreatedDate":"date","Body":"*******"}
{"Id":"ID","ParentId":"parent","Name":"filename","OwnerId":"owner","CreatedDate":"date","Body":"*******"}
]

感谢您提供详细信息。
对于这个问题,我会尝试分别转换每一行并通过您的流程流式传输:

Get-Content C:\TEMP\largefile.json | ForEach-Object {
    $_ = $_.Trim().TrimStart('[').TrimEnd(']')
    if ($_) { $_ | ConvertFrom-Json }
}

如前所述,如果这些内存问题不会出现在 PowerShell core 中,我不会感到惊讶。如果可能的话,我建议你也试试看。