Home > Software design >  Powershell JSON Building
Powershell JSON Building

Time:12-05

I am having problems getting assembling this Json in Powershell

{
  "totalCount": 1,
  "list": [
    {
      "type": "ToggleLightingState",
      "order": 1,
      "delay": null,
      "postDelay": null,
      "name": "Toggle lighting state of light L-17E-611-KB-1",
      "parameters": {
        "relayIds": [],
        "curveType": null,
        "behavior": null,
        "duration": null,
        "useAssignedSpace": false,
        "spaceIds": [],
        "lightIds": [
          2408
        ],
        "spaceGroupIds": []
      }
    }
  ]
}

I am Iterating through an array using a for loop to fill in the values. I am just struggling to generate a list inside a list in JSON

    
    $ActionList = @{
        
    
    @(
         @{
         type = 'ToggleLightingState'
         order = 1
         delay = 'null'
         postDelay = 'null'
         name = $actionSets[$i][0]
      }
     ) 
  }    
  ConvertTo-Json -InputObject $ActionList      

CodePudding user response:

Using this ConvertTo-Expression cmdlet:

$Json = @'
{
  "totalCount": 1,
  "list": [
    {
      "type": "ToggleLightingState",
      "order": 1,
      "delay": null,
      "postDelay": null,
      "name": "Toggle lighting state of light L-17E-611-KB-1",
      "parameters": {
        "relayIds": [],
        "curveType": null,
        "behavior": null,
        "duration": null,
        "useAssignedSpace": false,
        "spaceIds": [],
        "lightIds": [
          2408
        ],
        "spaceGroupIds": []
      }
    }
  ]
}
'@

$Json | ConvertFrom-Json |ConvertTo-Expression

[pscustomobject]@{
    totalCount = 1
    list = ,[pscustomobject]@{
        type = 'ToggleLightingState'
        order = 1
        delay = $Null
        postDelay = $Null
        name = 'Toggle lighting state of light L-17E-611-KB-1'
        parameters = [pscustomobject]@{
            relayIds = @()
            curveType = $Null
            behavior = $Null
            duration = $Null
            useAssignedSpace = $False
            spaceIds = @()
            lightIds = ,2408
            spaceGroupIds = @()
        }
    }
}

Or:

$Json |ConvertFrom-Json -AsHashTable |ConvertTo-Expression
@{
    totalCount = 1
    list = ,@{
        postDelay = $Null
        parameters = @{
            duration = $Null
            spaceGroupIds = @()
            relayIds = @()
            spaceIds = @()
            useAssignedSpace = $False
            curveType = $Null
            behavior = $Null
            lightIds = ,2408
        }
        type = 'ToggleLightingState'
        delay = $Null
        order = 1
        name = 'Toggle lighting state of light L-17E-611-KB-1'
    }
}

CodePudding user response:

You don't have the name of the array "list" inside the object. It looks like you can't have an unnamed array inside an object. I don't know what $actionsets is, so I took off the indexes. Plus fixing your syntax errors results in the below. Note that 'null' and $null are different things.

$ActionList = @{
  list = @(
    @{
      type = 'ToggleLightingState'
      order = 1
      delay = 'null'
      postDelay = 'null'
      name = $actionSets
    }
  )
}
ConvertTo-Json -InputObject $ActionList


{
    "list":  [
                 {
                     "delay":  "null",
                     "name":  null,
                     "postDelay":  "null",
                     "type":  "ToggleLightingState",
                     "order":  1
                 }
             ]
}
  • Related