我在CSV文件中有数百行,同时包含如下所示的JSON数据。下面是每行的示例。
{"Id":"value","RecordType":"value","CreationTime":"value","Operation":"value"}
我尝试将其转换为CSV格式,如下所示,但到目前为止还没有运气。
CSV文件的预期格式:
id RecordType CreationTime Operation value value value value
$properties = @('Id', 'RecordType', 'CreationTime', 'Operation')
(Get-Content -Path-to_CSVfile -Raw | ConvertFrom-Json) |
Select-Object -Property $properties |
Export-Csv -NoTypeInformation -Path $path-to-new-csv-file
如果有人对此有任何想法,请帮助我。我尝试了ConvertTo-Json
,但由于错误而失败:
ConvertFrom-Json:无效的JSON原语:“ id”
这是CSV数据的前两行。
{"Id":"ac325bc9-97f0-4b29-8fc4-90b80b945f6c","RecordType":20,"CreationTime":"2019-09-14T08:07:22","Operation":"AnalyzedByExternalApplication","OrganizationId":"f38a5ecd-2813-4862-b11b-ac1d563c806f","UserType":0,"UserKey":"3fee8456-6d20-4794-8219-5a7c381e965f","Workload":"PowerBI","UserId":"abcd@mail.com","ClientIP":"000.000.50.177","UserAgent":"MSOLAP 15.0 Client","Activity":"AnalyzedByExternalApplication","ItemName":"Other","DatasetName":"XYZ Driven Company","ObjectId":"Other","IsSuccess":true,"RequestId":"6836be8e-6e97-4bc9-a838-bf6e7b71e0c8","ActivityId":"7E92AE6A-F548-448D-93A8-6F5736DEA085"} {"Id":"3a20c8a9-ef44-483a-b9c0-43e10deae9ae","RecordType":20,"CreationTime":"2019-09-14T08:07:20","Operation":"AnalyzedByExternalApplication","OrganizationId":"f38a5ecd-2813-4862-b11b-ac1d563c806f","UserType":0,"UserKey":"3fee8456-6d20-4794-8219-5a7c381e965f","Workload":"PowerBI","UserId":"abcd@mail.com","ClientIP":"000.000.50.177","UserAgent":"MSOLAP 15.0 Client","Activity":"AnalyzedByExternalApplication","ItemName":"Other","DatasetName":"XYZ Driven Company","ObjectId":"Other","IsSuccess":true,"RequestId":"02e5d772-057b-45b6-ae60-22b7fa610f98","ActivityId":"7E92AE6A-F548-448D-93A8-6F5736DEA085"}
我正在另一个CSV文件中查找此数据,如下所示。 “:”之后的每个值都应作为行插入CSV。
Id RecordType CreationTime Operation OrganizationId UserType UserKey Workload UserId ClientIP UserAgent Activity ItemName DatasetName ObjectId IsSuccess RequestId ActivityId ac325bc9-97f0-4b29-8fc4-90b80b945f6c 20 2019-09-14T08:07:22 AnalyzedByExternalApplication f38a5ecd-2813-4862-b11b-ac1d563c806f 0 3fee8456-6d20-4794-8219-5a7c381e965f PowerBI abcd@mail.com 000.000.50.177 MSOLAP 15.0 Client AnalyzedByExternalApplication Other xyz Driven Company Other TRUE 6836be8e-6e97-4bc9-a838-bf6e7b71e0c8 7E92AE6A-F548-448D-93A8-6F5736DEA085 3a20c8a9-ef44-483a-b9c0-43e10deae9ae 20 2019-09-14T08:07:20 AnalyzedByExternalApplication f38a5ecd-2813-4862-b11b-ac1d563c806f 0 3fee8456-6d20-4794-8219-5a7c381e965f PowerBI abcd@mail.com 000.000.50.177 MSOLAP 15.0 Client AnalyzedByExternalApplication Other XYZ Driven Company Other TRUE 02e5d772-057b-45b6-ae60-22b7fa610f98 7E92AE6A-F548-448D-93A8-6F5736DEA085
在文本编辑器中打开时,从CSV纠正数据。
“ {”“ Id”“:”“ ac325bc9-97f0-4b29-8fc4-90b80b945f6c”“,”“ RecordType”“:20,”“ CreationTime”“:”“” 2019-09-14T08:07:22 “”,“” Operation“”:“” AnalyzedByExternalApplication“”,“” OrganizationId“”:“” f38a5ecd-2813-4862-b11b-ac1d563abchrf“”,“” UserType“”:0,“” UserKey“”:“ “ 3fee8456-6d20-4794-8219-5a7c38abcdfe”“,”“工作负载”“:”“ Pxyswer”“,”“ UserId”“:”“ abcd@mail.com”“,”“ ClientIP”“:”“ 123.456 .50.177“”,“” UserAgent“”:“” MSOLAP 15.0客户端“”,“” Activity“”:“” AnalyzedByExternalApplication“”,“” ItemName“”:“”其他“”,“”数据集名称“”:“ “ ABCD驱动公司”“,”“ ObjectId”“:”“其他”“,”“ IsSuccess”“:true,”“ RequestId”“:”“ 6836be8e-6e97-4bc9-a838-bf6e7b71e0c8”,“” ActivityId “”:“” 7E92AE6A-F548-448D-93A8-6F5736DEA085“”}“
答案 0 :(得分:0)
如果输入文件仅包含单个示例数据行,则您发布的代码将起作用。如果输入文件包含多个类似的语句,则您的代码将无法使用,因为它是无效的JSON数据。
有效JSON:
{"Id":"value","RecordType":"value","CreationTime":"value","Operation":"value"}
有效JSON:
[
{"Id":"value","RecordType":"value","CreationTime":"value","Operation":"value"},
{"Id":"value","RecordType":"value","CreationTime":"value","Operation":"value"}
]
无效的JSON:
{"Id":"value","RecordType":"value","CreationTime":"value","Operation":"value"}
{"Id":"value","RecordType":"value","CreationTime":"value","Operation":"value"}
要转换后一种输入数据,您需要将每一行转换为一个单独的JSON文档:
$properties = 'Id', 'RecordType', 'CreationTime', 'Operation'
Get-Content 'C:\path\to\input.csv' |
ConvertFrom-Json |
Select-Object $properties |
Export-Csv 'C:\path\to\output.csv' -NoType
要导出除特定字段之外的所有输入字段,您需要定义要排除的属性,而不是要包括的属性:
$exclude = 'foo', 'bar'
Get-Content 'C:\path\to\input.csv' |
ConvertFrom-Json |
Select-Object -Properties * -Exclude $exclude |
Export-Csv 'C:\path\to\output.csv' -NoType
编辑:
显然,您的输入文件是只有一列且没有标题的CSV,因此您可以通过Import-Csv
导入它,但是您需要自己指定列标题。扩展该字段以获取各个JSON值,然后按照上述步骤进行操作。
$properties = 'Id', 'RecordType', 'CreationTime', 'Operation'
Import-Csv 'C:\path\to\input.csv' -Header foo |
Select-Object -Expand foo |
ConvertFrom-Json |
Select-Object $properties |
Export-Csv 'C:\path\to\output.csv' -NoType
如果要导出所有JSON值,只需省略Select-Object $properties
步骤。