Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating a JSON string, PowerShell object

I am unable to create a variable on this request so I can later convert the variable to JSON using converttojson

{
    "update": {
        "comment": [
            {
                "add": {
                    "body": "Comment added when resolving issue"
                }
            }
        ]
    },

    "transition": {
        "id": "21"
    }
}

Tried the below

$jsonRequest = @{
    update= @{
        comment =@{
           add =@{
               body = "$Description"
            }
        }

    }
    transition =@{
        id = $TransactionID
    }
}

But get an output as below

{
    "transition":  {
                       "id":  1
                   },
    "update":  {
                   "comment":  {
                                   "add":  "System.Collections.Hashtable"
                               }
               }
}
like image 534
Sudheej Avatar asked Jun 16 '17 20:06

Sudheej


1 Answers

Comment" in your JSON is a list containing a hashtable, in your code it's a hashtable containing a hashtable.

This looks right by making it an array of one item:

$jsonRequest = [ordered]@{
    update= @{
        comment = @(
            @{
               add =@{
                   body = "$Description"
                }
            }
        )
    }
    transition = @{
        id = 21
    }
}

$jsonRequest | ConvertTo-Json -Depth 10

And I've made it '[ordered]' so the update and transition come out in the same order as your code, although that shouldn't really matter.

like image 84
TessellatingHeckler Avatar answered Oct 07 '22 01:10

TessellatingHeckler