gpt4 book ai didi

json - 合并两个json对象

转载 作者:行者123 更新时间:2023-12-01 04:44:30 25 4
gpt4 key购买 nike

我有以下输入 - 2 个 json 文件,一个是基础文件,第二个包含相同的属性但不同的值,我想合并这些对象。

例如:

{
a:{
b:"asda"
}
c: "asdasd"
}

第二个文件:
{
a:{
b:"d"
}
}

结果应该是这样的:
{a:{b:"d"},c:"asdasd"}

用powershell可以吗?

最佳答案

加入(Join-Object)不是内置的CmdLet

这是@Mark 答案的扩展,它也通过子对象递归。

function merge ($target, $source) {
$source.psobject.Properties | % {
if ($_.TypeNameOfValue -eq 'System.Management.Automation.PSCustomObject' -and $target."$($_.Name)" ) {
merge $target."$($_.Name)" $_.Value
}
else {
$target | Add-Member -MemberType $_.MemberType -Name $_.Name -Value $_.Value -Force
}
}
}

merge $Json1 $Json2

$Json1

关于json - 合并两个json对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45549909/

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