How to add square brackets to JSON variable?

Welcome Forums General PowerShell Q&A How to add square brackets to JSON variable?

Viewing 3 reply threads
  • Author
    Posts
    • #245316
      Participant
      Topics: 13
      Replies: 40
      Points: 277
      Helping Hand
      Rank: Contributor

      I’m trying to add Work items to Azure DevOps from PowerShell via the REST API.
      If I manually type in my JSON it works fine; for example:

      ": "add", "path": "/fields/System.Title", "value": "$($WorkItemTitle)" } ]"[/crayon]

      However if I try to use ConvertTo-JSON bby doing this:

      Then my JSON looks deceptively similar:

      But since the square brackets, [ & ], are missing, the JSON isn’t accepted by Azure DevOps. Add the brackets, and all is fine.
      Obviously it is too much to ask for, that Microsoft would accept how PowerShell outputs JSON, so does anyone have a clever idea to how to add those brackets?
      I would prefer to not have to type in JSON code as it is too easy to introduce arrors, and really, it just shows off another weakness in PowerShell -__-

    • #245319
      Participant
      Topics: 13
      Replies: 40
      Points: 277
      Helping Hand
      Rank: Contributor

      Hah hah, I can see that PowerShell.org also doesn’t like JSON 😉

    • #245322
      Participant
      Topics: 15
      Replies: 1776
      Points: 3,218
      Helping Hand
      Rank: Community Hero

      Square brackets represent an array:

      Output:

    • #245328
      Participant
      Topics: 13
      Replies: 40
      Points: 277
      Helping Hand
      Rank: Contributor

      Square brackets represent an array:

      [pscustomobject]@{ SomeProperty = ‘Foo’ AnotherProperty = @(‘Fe’,’Fi’,’Fum’) } | ConvertTo-Json Output:

      { “SomeProperty”: “Foo”, “AnotherProperty”: [ “Fe”, “Fi”, “Fum” ] }

      Yes, that is correct, fortunately I found a solution on Reddit.

Viewing 3 reply threads
  • You must be logged in to reply to this topic.