PowerShell to form correctly formatted json for ca

2019-07-25 15:10发布

I am struggling to get PowerShell to form a correctly formatted json entry when creating a new Google Calendar entry.

A correctly configured json, that is accepted by GC is as follows:

'{"end": {"dateTime": "2017-06-07T15:00:00Z"},
 "start": {"dateTime": "2017-06-07T10:00:00Z"}
}'

However, forming a json from a hashtable (so I can use a variable dateTime) does not form it correctly, example:

$body=@{
    start="2017-06-08T10:00:00Z"
    end="2017-06-08T12:00:00Z"}

$json = $body | ConvertTo-Json

$json is returned as follows:

{
    "start":  "2017-06-08T10:00:00Z",
    "end":  "2017-06-08T12:00:00Z"
}

This is not accepted by the Google Cal API, an when testing with the OAuth 2.0 Playground it returns the following error:

{
  "error": {
    "code": 400, 
    "message": "Missing end time.", 
    "errors": [
     {
        "domain": "global", 
        "message": "Missing end time.", 
        "reason": "required"
     }
   ]
 }

Any ideas on how to use PowerShell to form a correctly formatted Start and End date?

Thanks :)

1条回答
Luminary・发光体
2楼-- · 2019-07-25 15:41

Using nesting:

$body=@{
 start=@{"datetime" = "2017-06-08T10:00:00Z"}
 end=@{"datetime" = "2017-06-08T12:00:00Z"}} | ConvertTo-Json

Outputs:

{
    "start":  {
                  "datetime":  "2017-06-08T10:00:00Z"
              },
    "end":  {
                "datetime":  "2017-06-08T12:00:00Z"
            }
}
查看更多
登录 后发表回答