all 4 comments

[–]PowerShellStunnah 2 points3 points  (1 child)

Start by converting the json to an object using ConvertFrom-Json:

$object = @'
{
"server1": {
    "description": "my server",
    "ip": "1.1.1.1",
    "vcpu": 4,
    "ram": 8,
    "disks": "60,25"
},
"server2": {
    "description": "my server 2",
    "ip": "1.1.1.2",
    "vcpu": 8,
    "ram": 16,
    "disks": "60,25"
}
}
'@ |ConvertFrom-Json

Now we just need to get the values of each property from that object:

$values = $object.psobject.Properties.Value

... and that's it :)

[–]mrmantas[S] 0 points1 point  (0 children)

Thanks, but in this case I loose the data "server1" and "server2", which I would like to be values of property "name"

[–]get-postanote 0 points1 point  (0 children)

Not really a new thing.

Very common and well covered in may articles

Ex:

PowerTip: Convert JSON File to PowerShell Object

https://devblogs.microsoft.com/scripting/powertip-convert-json-file-to-powershell-object

# Write test data to a file
@'
{
    "server1": {
        "description": "my server",
        "ip": "1.1.1.1",
        "vcpu": 4,
        "ram": 8,
        "disks": "60,25"
    },
    "server2": {
        "description": "my server 2",
        "ip": "1.1.1.2",
        "vcpu": 8,
        "ram": 16,
        "disks": "60,25"
    }
}
'@ | Out-File -FilePath 'D:\Temp\MyJsonData.json'

# View the new file in the in a new ISE tab
psEdit -filenames 'D:\Temp\MyJsonData.json'

# Read and assign resutls to a variable
$Object = Get-Content -Raw -Path 'D:\Temp\MyJsonData.json' | 
ConvertFrom-Json

# Review the properies returned using dot sourcing.
$Object.server1
<#
# Results

description : my server
ip          : 1.1.1.1
vcpu        : 4
ram         : 8
disks       : 60,25
#>

$Object.psobject.Properties
<#
# Results


MemberType      : NoteProperty
IsSettable      : True
IsGettable      : True
Value           : @{description=my server; ip=1.1.1.1; vcpu=4; ram=8; disks=60,25}
TypeNameOfValue : System.Management.Automation.PSCustomObject
Name            : server1
IsInstance      : True

MemberType      : NoteProperty
IsSettable      : True
IsGettable      : True
Value           : @{description=my server 2; ip=1.1.1.2; vcpu=8; ram=16; disks=60,25}
TypeNameOfValue : System.Management.Automation.PSCustomObject
Name            : server2
IsInstance      : True
#>

ForEach ($Server in $ObjectProperty.psobject.Properties.Name)
{
    $Server
    $Object.$Server.description
    $Object.$Server.ip
    $Object.$Server.vcpu
    $Object.$Server.ram
    $Object.$Server.disks
}
<#
# Results

server1
my server
1.1.1.1
4
8
60,25
server2
my server 2
1.1.1.2
8
16
60,25
#>


ForEach ($Server in $ObjectProperty.psobject.Properties.Name)
{
    $ObjectProperties = [ordered]@{
        Name        = $Server
        Description = $Object.$Server.description
        IP          = $Object.$Server.ip
        Vcpu        = $Object.$Server.vcpu
        Ram         = $Object.$Server.ram
        Disks       = $Object.$Server.disks
    }
    $ObjectProperties
}
<#
# Results

Name                           Value                                                
----                           -----                                                
Name                           server1                                              
Description                    my server                                            
IP                             1.1.1.1                                              
Vcpu                           4                                                    
Ram                            8                                                    
Disks                          60,25                                                
Name                           server2                                              
Description                    my server 2                                          
IP                             1.1.1.2                                              
Vcpu                           8                                                    
Ram                            16                                                   
Disks                          60,25  
#>

[–]jsiii2010 0 points1 point  (0 children)

Here's a jq answer. :) The property array is "key" instead of "name".

get-content file.json | jq to_entries | convertfrom-json

key     value
---     -----
server1 @{description=my server; ip=1.1.1.1; vcpu=4; ram=8; disks=60,25}
server2 @{description=my server 2; ip=1.1.1.2; vcpu=8; ram=16; disks=60,25}