0

friends! Starting to work with powershell and encountered a problem. I need to output the values of some variables in a JSON format string in a loop. For this, I use the following code:

.....
    foreach ($value in $memoryLast5min)
    {
        $valueInt = [convert]::ToInt32($value, 10);   
        if ($valueInt -ge $500MB)
        {
            $index = [array]::IndexOf($memoryLast5min, $value);        
            $JSON += @{'problemUsers' = @(
                   @{
                        "userName"       = $userNames[$index]
                        "memoryLast5min" = $value
                        "readLast5min"   = $readLast5min[$index]
                        "writeLast5min"  = $writeLast5min[$index]
                    } 
                ) 
            } | ConvertTo-JSON;
        }
    }
.....
return $JSON

In the variable $JSON I get the following result:

{
    "problemUsers":  [
                         {
                             "userName":  "User1",
                             "writeLast5min":  29,
                             "memoryLast5min":  181,
                             "readLast5min":  25
                         }
                     ]
}{
    "problemUsers":  [
                         {
                             "userName":  "User2",
                             "writeLast5min":  80,
                             "memoryLast5min":  396,
                             "readLast5min":  74
                         }
                     ]
}{
    "problemUsers":  [
                         {
                             "userName":  "User3",
                             "writeLast5min":  32,
                             "memoryLast5min":  169,
                             "readLast5min":  29
                         }
                     ]
}

But I need this result:

{
    "problemUsers":  [
                         {
                             "userName":  "User1",
                             "writeLast5min":  29,
                             "memoryLast5min":  181,
                             "readLast5min":  25
                         },
                         {
                             "userName":  "User2",
                             "writeLast5min":  80,
                             "memoryLast5min":  396,
                             "readLast5min":  74
                         },
                         {
                             "userName":  "User3",
                             "writeLast5min":  32,
                             "memoryLast5min":  169,
                             "readLast5min":  29
                         }
                     ]
}

Please, help me fix the code

3
  • I’d try removing @{'problemUsers' = from your $json statement and the json conversion from your loop. Then outside of the loop do @{'problemUsers' = $json} | convertto-Json Commented Nov 6, 2019 at 15:27
  • @nzrytmn, my JSON file should look the same as in the 3rd code block of my question Commented Nov 6, 2019 at 15:37
  • @AdminOfThings thanks! It works! Outside of the loop do $JSON = @{'problemUsers' = $JSON} | convertto-Json Commented Nov 6, 2019 at 15:41

1 Answer 1

4

Build a data structure. When you're finished, convert to JSON.

$data = [pscustomobject]@{
    problemUsers = @()
}

foreach ($value in $memoryLast5min)
{
    $valueInt = [convert]::ToInt32($value, 10);   
    if ($valueInt -ge $500MB)
    {
        $index = [array]::IndexOf($memoryLast5min, $value) 
        $data.problemUsers += @{
            userName       = $userNames[$index]
            memoryLast5min = $value
            readLast5min   = $readLast5min[$index]
            writeLast5min  = $writeLast5min[$index]
        }
    } 
}

$json = $data | ConvertTo-Json
Sign up to request clarification or add additional context in comments.

4 Comments

I think you need to create custom objects from the hashtables inside the loop too, and $data.problemUsers = @(foreach (...) {...}) would avoid appending to an array in a loop.
Custom objects and hash tables should work the same, am I missing something?
No, you're right. I thought you needed objects, but I just checked and apparently hashtables work just the same.
I agree, $data.problemUsers = @(foreach (...) {...}) is more optimized for a single assignment, but then again, the OP might have more such sections where problem users are added based on different criteria, I did not want to preclude anything here.

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.