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"
]
1
  • 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 and many more on SO, and all over the web and on Youtube. Commented Apr 7, 2021 at 4:39

2 Answers 2

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"]}
Sign up to request clarification or add additional context in comments.

4 Comments

JPBlanc, Should this $fruits | ConvertTo-Json be this $obj| ConvertTo-Json, since $obj is what you created to hold that info to convert?
@postanote you are right, bad copy/past. Thanks
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.
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
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"
  ]
]
#>

1 Comment

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

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.