gpt4 book ai didi

json - 比较和更新Json数组

转载 作者:行者123 更新时间:2023-12-03 01:28:21 26 4
gpt4 key购买 nike

我有两组json文件:Parameters1.json和parameters2.json,如下所示:

Parameters1.json

{
"TypeofService":"CITService",
"ServiceName":"abc",
"SCNNAME":"abc_V1.scn",
"ScheduleInterval":"Daily",
"ScheduleDay":"MON,TUE,WED,THU,FRI,SAT",
"ScheduleTime":"08:30",
"Folder_structure": "Success\\test1,Success\\test2",
"CIT_Properties": [
{
"isPassword": false,
"Property": "host1",
"Value": "xyz"
},
{
"isPassword": false,
"Property": "Port1",
"Value": "8081"
},
{
"isPassword": false,
"Property": "user1",
"Value": "testuser"
},
{
"isPassword": true,
"Property": "password1",
"Value": "12345"
}
]
}

Parameters2.json(要更新的文件)
{
"TypeofService":"CITService",
"ServiceName":"abc",
"SCNNAME":"abc_V2.scn",
"ScheduleInterval":"Daily",
"ScheduleDay":"MON,TUE,WED,THU,FRI,SAT",
"ScheduleTime":"08:30",
"Folder_structure": "Success\\test1,Success\\test2",
"CIT_Properties": [
{
"isPassword": false,
"Property": "host1",
"Value": "xyz"
},
{
"isPassword": false,
"Property": "port1",
"Value": "8080"
},
{
"isPassword": false,
"Property": "user1",
"Value": "generic"
},
{
"isPassword": true,
"Property": "password1",
"Value": "56789"
},
{
"isPassword": false,
"Property": "host2",
"Value": "xyz2"
},
{
"isPassword": false,
"Property": "port2",
"Value": "8080"
},
{
"isPassword": false,
"Property": "user2",
"Value": "user2"
},
{
"isPassword": true,
"Property": "password2",
"Value": "1234567890"
}
]
}

我要实现的目标是,如果两个文件中的CIT_Properties中的“属性”都匹配,则应该在parameters2.json中更新parameter1.json中对应的CIT_properties.value。

即,如果您在上面看到:两个文件中的port1,user1和password1是公用的。我希望将值(parameters2.json中的8080,generic,56789)替换为(8081,testuser,12345)。

到目前为止,我所做的是:
$json1 = (Get-Content "C:\Users\parameters1.json" -Raw) | Out-String | ConvertFrom-Json
$json2=(Get-Content "C:\Users\parameters2.json" -Raw) | Out-String | ConvertFrom-Json

for ($a = 0; $a -lt $json2.PsObject.properties.value.Property.length; $a++)
{
for ($b = 0; $b -lt $json1.PsObject.properties.value.Property.length; $b++)
{
if ($json2.PsObject.properties.value.Property[$a] -eq $json1.PsObject.properties.value.Property[$b])
{
$json2.PsObject.properties.value.Value[$a]= $json1.PsObject.properties.value.Value[$b]
}
}
}

我看到我可以访问各个值,但是当我尝试设置它们时,会抛出错误。

最佳答案

Where-Object是您的 friend 在这里。
如果循环遍历文件1中的较小数组,则效率会更高。
不需要嵌套循环,因为“对象”应该可以更快地工作。
此文件已在Powershell Core中进行了测试,但应在所有版本中均可使用。
您可以使用Set-Content更新您的json文件或通过变量$ j2创建一个新文件。

$j1 = Get-Content ./parameters1.json -Raw | ConvertFrom-Json
$j2 = Get-Content ./parameters2.json -Raw | ConvertFrom-Json

foreach ($Obj in $j1.CIT_Properties)
{
($j2.CIT_Properties | where {$_.Property -eq $Obj.Property}).Value = $Obj.Value
}

编辑:
添加了示例以回答评论。
该答案假定如果script_properties在j1中存在,则在j2中也存在,如果您更新问题中的json,我可以扩展答案。

存在于j2中。
$j1 = Get-Content ./J1.json -Raw | ConvertFrom-Json
$j2 = Get-Content ./J2.json -Raw | ConvertFrom-Json

foreach ($Obj in $j1.CIT_Properties)
{
($j2.CIT_Properties | where {$_.Property -eq $Obj.Property}).Value = $Obj.Value
}

if ($j1.script_properties)
{
if($j2.script_properties)
{
foreach ($Obj in $j1.CIT_Properties)
{
($j2.script_properties | where {$_.Property -eq $Obj.Property}).Value = $Obj.Value
}
}
else
{
$j2 | Add-Member -MemberType NoteProperty -Name script_properties -Value $j1.script_properties
}
}

编辑:我认为这是您正在寻找。我添加了一个条件来检查j2数组是否包含$ obj
$j1 = Get-Content ./J1.json -Raw | ConvertFrom-Json
$j2 = Get-Content ./J2.json -Raw | ConvertFrom-Json

foreach ($Obj in $j1.CIT_Properties)
{
if ($j2.CIT_Properties -contains $obj)
{
($j2.CIT_Properties | where {$_.Property -eq $Obj.Property}).Value = $Obj.Value
}
}

if ($j1.script_properties)
{
if($j2.script_properties)
{
foreach ($Obj in $j1.CIT_Properties)
{
if ($j2.script_Properties -contains $obj)
{
($j2.script_properties | where {$_.Property -eq $Obj.Property}).Value = $Obj.Value
}
}
}
else
{
$j2 | Add-Member -MemberType NoteProperty -Name script_properties -Value $j1.script_properties
}
}

关于json - 比较和更新Json数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60482510/

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