Create variable with mutiples tables

asked 2020-08-21T15:26:08.487+00:00
Steven N 96 Reputation points

Hello,

I do not know how to phrase that question correctly so I will try with example.

Here is the JSON example of what I'd like to recreate in PowerShell format:

[
    {
        id : "001",
        name : "apple",
        category : "fruit",
        color : "red"
    }, 
...
]

I am not sure if it's possible to do the same in PowerShell.

Thanks,
Steven

Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
4,607 questions
No comments
{count} votes

Accepted answer
  1. answered 2020-08-21T15:46:01.117+00:00
    Steven N 96 Reputation points

    Hello,

    I found the answer.

    $CLIENTTABLE = [ordered]@{
    client = @{
    name = ("HELLO",'WORLD')
    ipaddress = ("1.1.1.1",'2.2.2.2')}}

    Thanks,
    Steven

    No comments

1 additional answer

Sort by: Most helpful
  1. answered 2020-08-21T18:33:46.357+00:00
    Rich Matheisen 34,791 Reputation points

    I don't think you found the correct answer . . . not if your original question had the structure you're after. Try this instead:

    $CLIENTTABLE1 = @(
        [ordered]@{
                    name = "HELLO"
                    ipaddress = "1.1.1.1"
                  },
        [ordered]@{
                    name = "WORLD"
                    ipaddress = "2.2.2.2"
                  }
    )
    $CLIENTTABLE1 | ConvertTo-Json
    

    That produces this:

    [
        {
            "name":  "HELLO",
            "ipaddress":  "1.1.1.1"
        },
        {
            "name":  "WORLD",
            "ipaddress":  "2.2.2.2"
        }
    ]
    
    No comments