Quantcast
Channel: PowerShell.com – PowerShell Scripts, Tips, Forums, and Resources
Viewing all articles
Browse latest Browse all 6937

hash tables square brackets and convertto-json

$
0
0

Hi usually I find I can work things like this out, but this one has had me turning circles for days :(

I am sure the answer is pretty easy and something I have missed , but I have tried all types of character escaping and I am at my wits end.

I am trying to do a post to a JSON web service. I create a hashtable and populate with some values.

The JSON request requires a "params" field with a nested hashtable within in it ("filter") as well as a filed called method (just a string) that's passed in

So I use the code below and I can post the data no problem

$post= @{"jsonrpc"="2.0";

 

 

"method"=$method

 

 

 

 

"params"= @{"filter"= @{}}

}

$post = $post | convertto-json

The issue is the above code works fine when the filter is blank ({}). The problem arises as the web service expects a JSON array with key-pairs within the filter (hence the reason for the nested hastable)

 

e.g.

"params"= @{"filter"= @{"eventid" = ["1"]}}

The problem is powershell doesn't like square brackets within the nested hashtable (filter) as its expecting a type declaration. So I try all different methods of escaping the characters and quoting. But I cannot get both powershell to accept the hashtable value with square brackets included or when I do manage to do this it screws up the JSON formatted data with "/"slashes or character codes.

The JSON should look like this:

{
"jsonrpc": "2.0",
"method": "mymethod",
"params": {
"eventtId": [
"1"
]

}

 Any help would be much appreciated I am sure it's just a formatting issue but for the life of me I cannot get it work :)

 

 


Viewing all articles
Browse latest Browse all 6937

Trending Articles