How to combine this JSON in PSObject and back in Powershell

Powershell does not seem to be able to correctly round this JSON object:

{ "settings": { "minimumApproverCount": 2, "creatorVoteCounts": false, "scope": [ { "refName": "refs/heads/d14rel", "matchKind": "Exact", "repositoryId": "a290117c-5a8a-40f7-bc2c-f14dbe3acf6d" } ] } } 

Assuming $json is a string, this command:

 $json | ConvertFrom-Json | ConvertTo-Json 

outputs the wrong JSON from it:

 { "settings": { "minimumApproverCount": 2, "creatorVoteCounts": false, "scope": [ "@{refName=refs/heads/d14rel; matchKind=Exact; repositoryId=a290117c-5a8a-40f7-bc2c-f14db e3acf6d}" ] } } 

Note that the variable "scope" is incorrect. Is there any way to fix this?

+8
json powershell
source share
1 answer

Use the Depth parameter with a value of 3 or more. By default, 2 is not enough; deeper data is simply converted to strings.

 $json | ConvertFrom-Json | ConvertTo-Json -Depth 3 

Exit

 { "settings": { "minimumApproverCount": 2, "creatorVoteCounts": false, "scope": [ { "refName": "refs/heads/d14rel", "matchKind": "Exact", "repositoryId": "a290117c-5a8a-40f7-bc2c-f14dbe3acf6d" } ] } } 
+9
source share

All Articles