0

I wanted to have a JSON like this in PS.

{
    "blocks": [
        {
            "type": "section",
            "text": {
                "type": "mrkdwn",
                "text": "This is Random Text"
            }
        }
    ]
}

For which I write-up following code in my PS script.

$Json=@{
    "blocks"=@(
    @{
        "type"="section"
        "text"=@{
            "type"="mrkdwn"
            "text"="This is Random Text"
        }
    }
    )
}

But when I convert it to JSON, I get following output.

{
    "blocks":  [
                   {
                       "text":  "System.Collections.Hashtable",
                       "type":  "section"
                   }
               ]
}

I am pretty much novice with PS, any help would be highly appreciated.

  • You can resolve the `"System.Collections.Hashtable"` issue by specifying `-Depth` when calling `ConvertTo-Json`. To retain the order of the properties, use an ordered dictionary instead of a regular hashtable (`@{ ... }` -> `[ordered]@{ ... }`) – Mathias R. Jessen Feb 24 '23 at 11:48
  • Thanks a ton Mathias, you saved my day. Additional thanks for pointing out about retaining the order of the properties and how to keep it same in PS. – Ahsan Abbas Feb 24 '23 at 11:53

0 Answers0