将嵌套的 JSON 数组转换为 CSV 文件中的单独列

编程入门 行业动态 更新时间:2024-10-27 02:17:30
本文介绍了将嵌套的 JSON 数组转换为 CSV 文件中的单独列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个如下所示的 JSON 文件:

I have a JSON file that looks like this:

{ "id": 10011, "title": "Test procedure", "slug": "slug", "url": "test.test", "email": "test@test", "link": "test.er", "subject": "testing", "level": 1, "disciplines": [ "discipline_a", "discipline_b", "discipline_c" ], "areas": [ "area_a", "area_b" ] },

我试图使用以下命令将其转换为 CSV 文件:

I was trying to use the following command to convert that into the CSV file:

(Get-Content "PATH_TO est.json" -Raw | ConvertFrom-Json)| Convertto-CSV -NoTypeInformation | Set-Content "PATH_TO est.csv"

但是,对于学科和领域,我在生成的 CSV 文件中获取 System.Object[].

However, for disciplines and areas I am getting System.Object[] in the resulting CSV file.

有没有办法将所有这些嵌套值作为单独的列放在 CSV 文件中,例如 area_1、area_2 等.对于学科也是如此.

Is there a way to put all those nested values as a separate columns in CSV file like area_1, area_2 etc. And the same for disciplines.

推荐答案

CSV 转换/导出 cmdlet 无法展平"对象,我可能会遗漏一些东西,但我不知道有什么办法可以用内置 cmdlet 或功能.如果您可以保证 disciplines 和 areas 将始终具有相同数量的元素,则可以通过使用带有派生属性的 Select-Object 来简化它这样做:

The CSV conversion/export cmdlets have no way of "flattening" an object, and I may be missing something, but I know of no way to do this with a built-in cmdlet or feature. If you can guarantee that disciplines and areas will always have the same number of elements, you can trivialize it by using Select-Object with derived properties to do this:

$properties=@('id','title','slug','url','email','link','subject','level', @{Name='discipline_1';Expression={$_.disciplines[0]}} @{Name='discipline_2';Expression={$_.disciplines[1]}} @{Name='discipline_3';Expression={$_.disciplines[2]}} @{Name='area_1';Expression={$_.areas[0]}} @{Name='area_2';Expression={$_.areas[1]}} ) (Get-Content 'PATH_TO est.json' -Raw | ConvertFrom-Json)| Select-Object -Property $properties | Export-CSV -NoTypeInformation -Path 'PATH_TO est.csv'

但是,我假设 disciplines 和 areas 对于每条记录都是可变长度的.在这种情况下,您必须循环输入并提取学科和领域的最高计数值,然后动态构建属性数组:

However, I am assuming that disciplines and areas will be variable length for each record. In that case, you will have to loop over the input and pull the highest count value for both disciplines and areas, then build the properties array dynamically:

$inputData = Get-Content 'PATH_TO est.json' -Raw | ConvertFrom-Json $counts = $inputData | Select-Object -Property @{Name='disciplineCount';Expression={$_.disciplines.Count}},@{Name='areaCount';Expression={$_.areas.count}} $maxDisciplines = $counts | Measure-Object -Maximum -Property disciplineCount | Select-Object -ExpandProperty Maximum $maxAreas = $counts | Measure-Object -Maximum -Property areaCount | Select-Object -ExpandProperty Maximum $properties=@('id','title','slug','url','email','link','subject','level') 1..$maxDisciplines | % { $properties += @{Name="discipline_$_";Expression=[scriptblock]::create("`$_.disciplines[$($_ - 1)]")} } 1..$maxAreas | % { $properties += @{Name="area_$_";Expression=[scriptblock]::create("`$_.areas[$($_ - 1)]")} } $inputData | Select-Object -Property $properties | Export-CSV -NoTypeInformation -Path 'PATH_TO est.csv'

此代码尚未经过全面测试,因此可能需要进行一些调整才能 100% 工作,但我相信这些想法是可靠的 =)

This code hasn't been fully tested, so it may need some tweaking to work 100%, but I believe the ideas are solid =)

更多推荐

将嵌套的 JSON 数组转换为 CSV 文件中的单独列

本文发布于:2023-10-30 07:53:31,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1542239.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:嵌套   数组   转换为   文件   JSON

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!