1

I have an array that I created this way

[System.Collections.ArrayList]$Fruits = "A", "B"

Now I want to have a Json document that looks likd this

{
'fruits':['A', 'B']
}

I have no idea how to create the key as

$fruits | ConvertTo-Json

simply creates this

[
    "A",
    "B"
]
Tom Martens
  • 118
  • 7
  • BTW, we should have pointed you to this before, we responded; because your use case is not really a new thing as per the MSDoc Powershell help file pointed to for PSCore. However, this is alas [a duplicate question as per this SO Q&A](https://stackoverflow.com/questions/58617070/how-to-convert-powershell-array-to-json-using-convertto-json) and many more on SO, and all over the web and on Youtube. – postanote Apr 07 '21 at 04:39

2 Answers2

1

you should build an object with a fruit property :

$obj = New-Object psobject -Property @{fruits = "A", "B"}
$obj | ConvertTo-Json

Another way :

$obj = "" | Select-Object -Property fruits
$obj.fruits =  "A", "B"
$obj | ConvertTo-Json

gives :

{
    "fruits":  [
                   "A",
                   "B"
               ]
}

Be careful the original Depth is 3, you shouls give the one you need and use -compress :

$obj | ConvertTo-Json -Depth 5 -Compress 

It gives :

{"fruits":["A","B"]}
JPBlanc
  • 70,406
  • 17
  • 130
  • 175
  • `JPBlanc`, Should this `$fruits | ConvertTo-Json` be this `$obj| ConvertTo-Json`, since $obj is what you created to hold that info to convert? – postanote Apr 07 '21 at 03:52
  • @postanote you are right, bad copy/past. Thanks – JPBlanc Apr 07 '21 at 04:31
  • No worries, good stuff for those still on WinPS. Yet, We should have just hit the SP search box, or told the OP to do so, and closed this as a duplicate question, since it is a duplicate of a few Q&As with accepted answers. Oh, well, next time. – postanote Apr 07 '21 at 04:36
  • thanks @JPBlanc I wasn't aware that I had to create an object with a property named like the key I needed inside the Json document. On a 2nd thought this makes totally sense. So, your answer helped me a lot – Tom Martens Apr 07 '21 at 07:06
0

You are not saying what version of PowerShell you are using. So, to tagginb on to the helpful answer from @JPBlanc; if this is on PowerShell Core (PSv7), then this.

ConvertTo-Json

https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/convertto-json?view=powershell-7.1

$Host
# Results
<#
Name             : ConsoleHost
Version          : 7.1.3
InstanceId       : 1eed2704-3482-499f-b86d-d138ff405ad4
UI               : System.Management.Automation.Internal.Host.InternalHostUserInterface
CurrentCulture   : en-US
CurrentUICulture : en-US
PrivateData      : Microsoft.PowerShell.ConsoleHost+ConsoleColorProxy
DebuggerEnabled  : True
IsRunspacePushed : False
Runspace         : System.Management.Automation.Runspaces.LocalRunspace
#>

(Get-Command -Name ConvertTO-Json).Parameters.Keys
# Results
<#
InputObject
Depth
Compress
EnumsAsStrings
AsArray
EscapeHandling
Verbose
Debug
ErrorAction
WarningAction
InformationAction
ErrorVariable
WarningVariable
InformationVariable
OutVariable
OutBuffer
PipelineVariable
#>


 [System.Collections.ArrayList]$Fruits = 'A', 'B'
 ConvertTo-Json -AsArray @($Fruits) -Compress
# Results
<#
[["A","B"]]
#>

Or

ConvertTo-Json -AsArray @($Fruits)
# Results
<#
[
  [
    "A",
    "B"
  ]
]
#>
postanote
  • 15,138
  • 2
  • 14
  • 25
  • thanks for taking the time, but unfortunately I can't get your answer to create the Json I needed. I was looking for the missing object.property value link to the json key:value paradigm from the JPBlanc's answer – Tom Martens Apr 07 '21 at 07:13