gpt4 book ai didi

json - 从 Json 解析数据数据文件中的值

转载 作者:行者123 更新时间:2023-12-04 08:02:22 25 4
gpt4 key购买 nike

我需要将一个 JSON 文件加载到 powershell 中,然后处理来自 JSon 文件的各个节点。该文件是从现有的 Json 文件加载的:

$JSONContent = (Get-Content -Path "C:\JsonFile.json" -Raw) | ConvertFrom-Json
加载后,我可以通过以下方式查看数据值:
$JSONContent | SELECT -expand Results
然后给我一个字段和值的列表格式。
我的问题是我想遍历 $JSONContent对象和每个逻辑记录(由逗号分隔),连接每个字段值以形成一个逗号分隔的字符串。
我确信答案是微不足道的,但我试图解决这个问题。如果哪位好心人能帮忙解决这个问题,我将不胜感激。
作为输入的 JSON 文件是:
{
"results": [
{
"id": 1,
"id2": null,
"first_name": "Jeanette",
"last_name": "Penddreth",
"email": "jpenddreth0@census.gov",
"gender": "Female",
"ip_address": "26.58.193.2",
"serverlist": [
{
"myval1": "testdata1",
"myval2": "testdata2",
"myval3": "testdata3"
}
],
"aitlist": [
{
"ait1": "aitdata1",
"ait2": "aitdata2"
}
]
},
{
"id": 2,
"first_name": "Giavani",
"last_name": "Frediani",
"email": "gfrediani1@senate.gov",
"gender": "Male",
"ip_address": "229.179.4.212",
"serverlist": [
{
"myval1": "testdata5",
"myval2": "testdata6",
"myval3": "testdata7"
},
{
"myval1": "testdata9",
"myval2": "testdata10",
"myval3": "testdata11"
}
],
"aitlist": [
{
"ait1": "aitdata3",
"ait2": "aitdata4"
}
]
},
{
"id": 3,
"first_name": "Noell",
"last_name": "Bea",
"email": "nbea2@imageshack.us",
"gender": "Female",
"ip_address": "180.66.162.255"
},
{
"id": 4,
"first_name": "Willard",
"last_name": "Valek",
"email": "wvalek3@vk.com",
"gender": "Male",
"ip_address": "67.76.188.26"
}
]
}
目标是使用第一条记录的示例,以逗号分隔的字符串形式结束每个节点的值:
1,"Jeanette","Penddreth","jpenddreth0@census.gov","Female", "26.58.193.2","testdata1","testdata2","testdata3","aitdata1","aitdata2"

最佳答案

以下命令通过 ConvertFrom-Json 递归地遍历从您的 JSON 输入解析的对象图中的所有数组和对象。并将每个对象的叶子属性值(包含原始类型的值)输出为逗号分隔的列表:

(Get-Content -Raw C:\JsonFile.json | ConvertFrom-Json).Results | ForEach-Object {
# Helper script block that walks an object graph and outputs all leaf property values.
$sb = {
foreach ($el in @($args[0])) {
if ($el -is [System.Management.Automation.PSCustomObject]) { # a complex object -> recurse
foreach ($prop in $el.psobject.Properties) { & $sb $prop.Value } # recurse on the properties
}
else {
$el # a leaf value -> output it
}
}
}

# Call the script block with the input object at hand and collect all values.
$values = & $sb $_

# Enclose string values in embedded "...", join with "," and output.
$values.ForEach({ if ($_ -is [string]) { "`"$_`"" } else { $_} }) -join ','

}
注意: @(...)附近 $args[0]需要确保 null JSON 值不会丢失。
使用您的样本输入,这会产生:
1,"Jeanette","Penddreth","jpenddreth0@census.gov","Female","26.58.193.2","testdata1","testdata2","testdata3","aitdata1","aitdata2"
2,"Giavani","Frediani","gfrediani1@senate.gov","Male","229.179.4.212","testdata5","testdata6","testdata7","testdata9","testdata10","testdata11","aitdata3","aitdata4"
3,"Noell","Bea","nbea2@imageshack.us","Female","180.66.162.255"
4,"Willard","Valek","wvalek3@vk.com","Male","67.76.188.26"

关于json - 从 Json 解析数据数据文件中的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66390256/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com