0

here is my json body .

{
    "source": 2,
    "revision": 3,
    "description": null,    
    "triggers": [],
    "releaseNameFormat": "Release-$(rev:r)",
    "tags": [],
    "pipelineProcess": {
        "type": 1
    },
    "properties": {
        "DefinitionCreationSource": {
            "$type": "System.String",
            "$value": "BuildSummary"
        },
        "System.EnvironmentRankLogicVersion": {
            "$type": "System.String",
            "$value": "2"
        }
    },
    "id": 5,
    "name": "CheckListAPI - CD",
    "path": "\\Admin",
    "projectReference": null,
    "url": "",
    "_links": {
        "self": {
            "href": ""
        },
        "web": {
            "href": ""
        }
    }
}

I want to add some values inside the brackets at "triggers": [], What I'm trying to get is:

"triggers": 
    [
    {
            "artifactAlias": "_DV_NJ_PIPE",
            "triggerConditions": [],
            "triggerType": 1
    }
    ],

i tried -replace and replace() saving the json file to local system, but none of them are working, I even tried to edit the json file directly like this but failed.

$alias = $json.triggers
foreach ($artifact in $alias )
{
$artifact.artifactAlias = "_$DefName"
$artifact.triggerConditions = "{}"
$artifact.triggertype = "artifactSource"
}

Please help.

1 Answer 1

2

You can import the json file as PowerShell objects, manipulate the structure until it looks the way you want it to and export it back to json format:

$pipeline = Get-Content .\input.json | ConvertFrom-Json

$trigger = [ordered]@{
    artifactAlias = "_DV_NJ_PIPE"
    triggerConditions = @()
    triggerType = 1
}
$pipeline.triggers += $trigger

$pipeline | ConvertTo-Json -Depth 5 | Out-File .\output.json

As it was pointed out in the comments, it is of course also possible to import the trigger definition from a json file instead of building it in a hash table.

Sign up to request clarification or add additional context in comments.

2 Comments

As the $trigger is also given in the question as a Json string, you could refer that (rather than rebuilding it from an ordered dictionary): $Trigger = ConvertFrom-Json '{"artifactAlias":"_DV_NJ_PIPE","triggerConditions":[],"triggerType":1}'
@iRon thanks, I misstyped the assignment operator. Its fixed now. Regarding import the trigger definition from json vs building it in a hashtable: In this case the latter seems more sensible to me.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.