简体   繁体   English

在powershell中合并json对象

[英]Merging json objects in powershell

I have json that looks like this: 我的json看起来像这样:

{
    "Workflow": [
        {
            "Parameters": {
                "Project": "/Path/To/File",
                "OtherParam": "True"
            }
        }
    ],
    "Overrides": [
        {
            "Special": {
                "Parameters": {
                    "NewParam": "NewStuffGoesHere",
                    "OtherParam": "False"
                }
            }
        }
    ]
}

... where I want to use the Overrides.Special section to add or update fields in the workflow object. ...我想使用Overrides.Special部分来添加或更新工作流对象中的字段。 In other words, given the json above, I want to do something like this: 换句话说,鉴于上面的json,我想做这样的事情:

$config = Get-Content workflow.json | out-string | ConvertFrom-Json
$configWithOverrides = Merge-Object $config.Workflow $config.Overrides.Special

And end up with something like this: 并最终得到这样的东西:

$configWithOverrides

Parameters
----------
@{Project=/Path/To/File; NewParam=NewStuffGoesHere; OtherParam=False}

I can certainly write the Merge-Object function above to add or update values as needed based on what's in the override section, but it seems there should (could?) be a built-in or one-liner way to handle this. 我当然可以根据覆盖部分中的内容编写上面的Merge-Object函数来根据需要添加或更新值,但似乎应该(可以?)是内置或单行方式来处理它。

I tried this: 我试过这个:

$test = $config.Workflow + $config.Overrides.Special

...but that doesn't quite work. ......但这不太有效。

$test
Parameters
----------
@{Project=/Path/To/File; OtherParam=True}
@{NewParam=NewStuffGoesHere; OtherParam=False}

This enables adding parameters: 这样可以添加参数:

>$test.Parameters.NewParam
NewStuffGoesHere

...but it's not so great for updating them ......但是更新它们并不是那么好

>$test.Parameters.OtherParam
True
False

Note - in this example, I'm choosing to handle the merge after converting the json to a psobject, but that's not a requirement. 注意 - 在这个例子中,我选择在将json转换为psobject之后处理合并,但这不是必需的。

I have a one-liner to do what you're asking for. 我有一个单行来做你想要的。 Notice that, as far as I know, PowerShell does not deal directly with json strings. 请注意,据我所知,PowerShell不直接处理json字符串。 But, once converted to PowerShell objects, it's like any other object. 但是,一旦转换为PowerShell对象,它就像任何其他对象一样。

So, firstly, define your json file, and read it as a single string: 因此,首先,定义您的json文件,并将其作为单个字符串读取:

# Requires -Version 4
$jsonFile='c:\temp\jsonfile.json'
$jsonObj=@(gc $jsonFile -raw)|ConvertFrom-Json

Define the property upon which you want to merge the json's objects, and the 1st and 2nd objects: 定义要合并json对象以及第1和第2个对象的属性:

$property='Parameters'
$1=$jsonObj.Workflow.$property
$2=$jsonObj.Overrides.Special.$property

Now, see the one-liner (which I've splitted in 3, for the sake of clarity): 现在,看一行(为了清楚起见,我将其拆分为3):

$MergedJson=[pscustomobject]@{
    $property=$2.psobject.properties|%{$11=$1}{$11|add-member $_.name   $_.value -ea Ignore}{$11}
}|ConvertTo-Json

You see? 你看? $MergedJson holds the following string (using your json string): $MergedJson保存以下字符串(使用您的json字符串):

{
    "Parameters":  {
                       "Project":  "/Path/To/File",
                       "OtherParam":  "True",
                       "NewParam":  "NewStuffGoesHere"
                   }
}

Is that what you're looking for? 这就是你要找的东西吗?

PS: if you swap the roles of $1 and $2, the common parameters' (like OtherParam ) values that prevail, change. PS:如果你换掉1美元和2美元的角色,普遍的参数'(比如OtherParam )值会占上风,变化。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM