4

How do I loop through all items in a JSON file? The current code just writes all names on one big line:

Get-Content -Raw -Path c:\temp\Environments.Generic.json | ConvertFrom-Json | ForEach-Object {
    Write-Host $_.Name
}

json file:

[
   {
      "Name":"EnableRetry",
      "Description":"Enable retry for Webservice Task",
      "Type":"Boolean",
      "Sensitive":false,
      "Value":true
   },
   {
      "Name":"FolderStageFiles",
      "Description":"Location of stage files",
      "Type":"String",
      "Sensitive":false,
      "Value":"d:\\sources\\"
   },
   {
      "Name":"FtpPassword",
      "Description":"Secret FTP password",
      "Type":"String",
      "Sensitive":true,
      "Value":"Welcome1"
   }
]
1

2 Answers 2

9

I ended up Select-Object and a ForEach-Object:

$JSON = Get-Content -Raw -Path c:\temp\Environments.Generic.json | ConvertFrom-Json

$JSON | Select-Object -Property Name,Description,Type,Sensitive,Value | ForEach-Object {
    Write-Host $_.Name $_.Value
}
Sign up to request clarification or add additional context in comments.

3 Comments

Looks like a fine solution to me. Apologies for not coming back to you.
No problem, I took your code as a base and that helped me... Will post the complete code on my blog in a couple of days.
Here is the complete code if someone want to copy it: microsoft-ssis.blogspot.com/2017/05/…
2

If you do this:

$JSON = Get-Content -Raw -Path c:\temp\Environments.Generic.json | ConvertFrom-Json

$JSON will be a PowerShell object that contains a collection of objects with all of the properties defined in your JSON file.

Enter $JSON at the console and you'll see the contents of this object.

To access specific properties for specific items in the collection you could do (for example):

$JSON | Where {$_.Name -eq 'FolderStageFiles'} | Select -ExpandProperty Value

1 Comment

Is there something for looping through the set because I don't know the values of the json file... only the structure (Name, Description, Type, Sensitive and Value). I want to loop through these records and add these values to an SSIS environment like microsoft-ssis.blogspot.com/2015/08/…

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.