0

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      
2
  • 4
    Could you please add a more complete, reproducible code sample? It's not really clear to me where exactly you are having problems. Commented Dec 4, 2022 at 18:05
  • 3
    Not sure I'm following what your question is about. Are you looking to rebuild this json from scratch using powershell? Commented Dec 4, 2022 at 18:05

2 Answers 2

1

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

3 Comments

Nice; if you use [ordered], you'll also be able to preserve entry-definition order.
@mklement0 yep or [pscustomobject], which is what it would normally convert back to
Yes, [pscustomobject] @{ ... } also preserves the definition order (despite seemingly using an unordered hashtable as input; this is yet to be documented as of this writing). Hashtables are more lightweight, however, so in scenarios where either type will do, (ordered) hashtables are usually preferable.
1

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 as hashtable:

$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'
    }
}

Comments

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.