2

Trying to send an api request and iterate trough the response but it seems my response isnt getting deserialize to objects .

This is the response im getting after invoke-Webrequest :

{"isSuccess": true, "value": null, "error": 0, "error2": ""}

Instead of:

Value error error2                                          IsSuccess
----- ---- -------                                           ---------

Here is the invoke im using :

$json = Invoke-WebRequest $RequestAPI-Method Post -Body $RequestBody -
ContentType 'application/json' | ConvertFrom-Json

Didnt post the API/Body as they are internal. Is this an issue with PowerShell? or am I getting a wrong type response? Im kind of confused.

If more info needed let me know and i'll try to add.

Martin Brandl
  • 56,134
  • 13
  • 133
  • 172
Udi
  • 49
  • 9
  • No luck... Stays the same. – Udi Nov 01 '17 at 11:49
  • Can you reveal/share the actual object returned with the [`PSON`](https://stackoverflow.com/a/24854277/1701026) cmdlet: `$Object = Invoke-WebRequest $RequestAPI-Method Post -Body $RequestBody - ContentType 'application/json'; PSON $Object -Layers 3 -Strict` – iRon Nov 01 '17 at 12:28
  • Im sorry but im not fimiliar with PSON and couldnt find information about it. When tried to put your command got : "the term 'PSON' is not recognized... – Udi Nov 01 '17 at 12:50
  • Sorry, I had the Parentheses on the wrong place. Retyped the comment: Try to rebuild the object: `New-Object PSObject (Invoke-WebRequest $RequestAPI-Method Post -Body $RequestBody - ContentType 'application/json' | ConvertFrom-Json)` – iRon Nov 01 '17 at 13:45
  • (I got a little confused because you call the object `$json`, instead it is an object converted *from* JSON) nevertheless, I guess that you got a JSON string returned rather then an object. So the question is: how doe the JSON file look like before you do a `ConvertTo-Json`? In other words: what is the result from just: `Invoke-WebRequest $RequestAPI-Method Post -Body $RequestBody -ContentType 'application/json'`? – iRon Nov 01 '17 at 13:56
  • Yes i have thought about it , and saw that the Content in the json response comes back like this : "{"isSuccess": true, "value": null, "error": 0, "error2": ""}" Theres the " at the start and end of the content. – Udi Nov 01 '17 at 15:19

1 Answers1

2

You probably have to select the Content property

$json = Invoke-WebRequest $RequestAPI-Method Post -Body $RequestBody -ContentType 'application/json' |
Select-Object -expand Content | 
ConvertFrom-Json
Martin Brandl
  • 56,134
  • 13
  • 133
  • 172
  • Hi Martin , Tried it but getting the same response : {"isSuccess": true, "value": null, "error": 0, "error2": ""} – Udi Nov 01 '17 at 09:41