1

I have looked through the past questions regarding updating a JSON file and can't seem to find exactly what i am looking for.

Lets start with the script itself.

foreach  ($Ip in $Ips)
{ 
     Code here that runs against IPs. 
foreach ($Cluster in $getCluster) { 
      Get-Cluster -name $Cluster | 
              select @{Name='attName';Expression {$IP}},
                     Name,
                     att1,
                     att2,
                     att3,
                     att4,
                     att5,
                     att6,
                     att7,
                     att8 | 
       Convertto-Json | out-file $ConfigFile.Results.ClusterOutput
       }
}

Now the first time this script runs the first time it hits the cluster section. It creates a .txt file where it houses the Json Data. The file looks like this:

First Run of ForEach $IP

[
    {
        "attName":  "1.1.1.1",
        "Name":  "testData",
        "att1":  value,
        "att2":  value,
        "att3":  value,
        "att4":  value,
        "att5":  value,
        "att6":  null,
        "att7":  null,
        "att8":  null
    },
    {
        "attName":  "2.2.2.2",
        "Name":  "testData",
        "att1":  value,
        "att2":  value,
        "att3":  value,
        "att4":  value,
        "att5":  value,
        "att6":  null,
        "att7":  null,
        "att8":  null
    }
]

Now the when the first foreach gets done with the second foreach i need it to add those attributes for the next IP address that comes up. I would like it to look like the following.

Second Run of ForEach $IP

[
    {
        "attName":  "1.1.1.1",
        "Name":  "testData",
        "att1":  value,
        "att2":  value,
        "att3":  value,
        "att4":  value,
        "att5":  value,
        "att6":  null,
        "att7":  null,
        "att8":  null
    },
    {
        "attName":  "2.2.2.2",
        "Name":  "testData",
        "att1":  value,
        "att2":  value,
        "att3":  value,
        "att4":  value,
        "att5":  value,
        "att6":  null,
        "att7":  null,
        "att8":  null
    },
    {
      This is the second runs data. 
        "attName":  "3.3.3.3",
        "Name":  "testData",
        "att1":  value,
        "att2":  value,
        "att3":  value,
        "att4":  value,
        "att5":  value,
        "att6":  null,
        "att7":  null,
        "att8":  null
    }
]

I feel like i need an if statement checking to see if the file is created - if it isn't then run it the first time - if it hasn't been written yet then add the "attributes to the existing file".

Here is what I have found and looking at right now. So far I haven't had success in making this work.

EDIT: I should have clarified a little bit better that was my bad. The information below were things i have been testing and trying to get to work. Thats not to say they work - its just things I have found as reference.

<# if (Test-Path $ConfigFile.Results.ClusterOutput){
        (New-Object PSObject |
            Add-Member -PassThru NoteProperty Name $getClusterConfig.Name |
            Add-Member -PassThru NoteProperty Age 10          |
            Add-Member -PassThru NoteProperty Amount 10.1     
        ) | Convertto-Json
        #pathToJson = "F:\Path\To\JSON\file.json"
        #        $a = Get-Content $ConfigFile.Results.ClusterOutput | ConvertFrom-Json
        #$a.policies.'Framework.DataContext'.connectionString = "Server=ServerName;Database=DateBaseName;Integrated Security=sspi2;"
        #$a | ConvertTo-Json | set-content $pathToJson
               }
        else{
      Get-Cluster -name $Cluster | 
              select @{Name='attName';Expression {$IP}},
                     Name,
                     att1,
                     att2,
                     att3,
                     att4,
                     att5,
                     att6,
                     att7,
                     att8 | 
       Convertto-Json | out-file $ConfigFile.Results.ClusterOutput
        }

My question is how can i get the if than else statement to add the next json child and keep it formatted like it should be. Any help would be greatly appreciated.

2
  • I'm a little confused here, is $a | ConvertTo-Json | set-content $pathtojson on the same line as $a = Get-content ? Commented May 17, 2017 at 3:16
  • Phil - the last part is just what i found and what i was looking at its not entirely working. Commented May 17, 2017 at 5:07

1 Answer 1

1

Ok, I think you want something like this for the json file appending:

#if file does exist read in json and append to json
if(Test-path "test.json"){
    $jsondata = Get-Content test.json | ConvertFrom-Json 
    #add hashtable of info to array with +=
    $jsondata += @{"Test"="ech";"ech"="test"}
}
#else file does not exist, create from scratch
else{
    #an array for the initial array of json dicts/hashmaps
    $jsondata = @()
    #add first hashtable with data
    $jsondata += @{"Test"="ech";"ech"="test"}
}
#overwrite file
ConvertTo-Json $jsondata | Out-File test.json 

I also think you might want to rethink your use of the pipe, the pipe is fine but it makes the code very hard to read and figure out what is going on.

Consider something like this:

else{
    #an array for the initial array of json hashtable
    $jsondata = @()
    $cluster = Get-Cluster -name $Cluster
    $jsondata += @{Name=$cluster.attName; Otherattr=$cluster.data} #etc
    ConvertTo-Json $jsondata | Out-File "filehere"

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

3 Comments

$GetJsonCluster = Get-Cluster -name $Cluster if(Test-path $ConfigFile.Results.ClusterOutput){ $jsondata = Get-Content $ConfigFile.Results.ClusterOutput | ConvertFrom-Json $jsondata += @{Name=$GetJsonCluster.Name; Otherattr=$GetJsonCluster.attr1} } else{ $jsondata = @() $jsondata += @{Name=$GetJsonCluster.Name; Otherattr=$GetJsonCluster.attr1} ConvertTo-Json $jsondata | Out-File $ConfigFile.vsan.Results.ClusterOutput } After running that i only get one response when there should be three.
Correction someone deployed a 4th cluster - so i should have 4 responses and the only thing that is added is the first run. The second run it doesn't write... so i may have something wrong in the first section of the If statement.
Figured out what i was missing. First if statement you had it read it in but never write it out. By adding this as the last line it actually wrote to file and added all 4 into it. ConvertTo-Json $jsondata | Out-File $ConfigFile.Results.ClusterOutput ---> OUTPUT AFTER adding that line: [ { "Otherattr": true, "Name": "cluster2" }, { "Otherattr": true, "Name": "cluster1" }, { "Otherattr": true, "Name": "bd_nested_6" }, { "Otherattr": true, "Name": "bd_nested" } ]

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.