4

Here is a small Powershell code snippet:

$users = New-Object System.Collections.ArrayList
$userAsJson = '
{
    "name" : "abc",
    "companies" : ["facebook", "google"]
}'
$user = $userAsJson | ConvertFrom-Json
$null = $users.Add($user)
$users | ConvertTo-Json -Depth 5

It gives me the following expected output:

{
    "name":  "abc",
    "companies":  [
                      "facebook",
                      "google"
                  ]
}

Now, I'm dynamically trying to create the companies list. I tried all possible things which I can think of. Here is what I have tried:

$company = New-Object System.Collections.ArrayList
$null = $company.Add('facebook')
$null = $company.Add('google')
$b = $company.ToArray()

$users = New-Object System.Collections.ArrayList
$userAsJson = '
{
    "name" : "abc",
    "companies" : $b
}'

$user = $userAsJson | ConvertFrom-Json
$null = $users.Add($user)

$users | ConvertTo-Json -Depth 5

Can anyone suggest me what is the best way to achieve it?

2
  • 1
    $user = @{name='abc'}; $user.companies = 'facebook', 'google'; $user | convertto-json - why are you trying to embedd the arraylist into a string then convert it from the string back into powershell? What's that for? Commented Jun 23, 2017 at 1:47
  • I am getting the companies list in a loop. It should be executed for each user. I just removed the loops for simplicity. How can i give companies list dynamically? Thanks in advance! Commented Jun 23, 2017 at 1:51

1 Answer 1

3

PowerShell's strength is in staying in the realm objects, until the time comes to interface with the outside world, such as when writing to a file or creating a string representation of these objects.

In your case that means:

# Array of companies; statically constructed here, but creating it
# dynamically works just as well.
$company = (
 'facebook',
 'google'
)

# Initialize the output collection.
# Note: Creating a [System.Collections.ArrayList] instance is
#       advisable for building up *large* arrays *incrementally*.
#       For smallish arrays, using regular PowerShell arrays will do; e.g.:
#         $users = @() # initialize array
#         $users += ... # append to array, but be aware that a *new* array 
#                         is created behind the scenes every time.
$users = New-Object System.Collections.ArrayList

# Add a user based on the $company array defined above as
# a [pscustomobject]
$null = $users.Add(
  [pscustomobject] @{
    name = 'abc'
    companies = $company
  }
)

# After all users have been added *as objects*, convert them to JSON.
$users | ConvertTo-Json -Depth 5

The above yields (based on a single object having been added; with more, a JSON array would be output):

{
  "name": "abc",
  "companies": [
    "facebook",
    "google"
  ]
}
Sign up to request clarification or add additional context in comments.

Comments

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.