none
Invoke-RestMethod: Case difference in json return of boolean values between between Powershell and curl/other methods? RRS feed

  • Question

  • Hi Technet/Powershell users - Just experienced an issue and wanted to report the issue to see if others have experienced this, or have any insight into what might be causing this.

    There is an API for a 3rd party software I am working with. The API endpoint I am querying can return properties of the object that include plain text or boolean values.

    When I query the API with Powershell's Invoke-RestMethod, the json return for the boolean values come back in uppercase. When I make the same call using curl or other methods, the returned boolean value is lowercase. Plaintext return doesn't seem to be affected/maintains case regardless of method.

    Caused a small issue as I set up the boolean property of my object in their software, retrieved the values via Invoke-RestMethod Method:Get and attempted to match the case of the property returned when making another POST call to the same API endpoint. Their software is case sensitive so the POST failed on unexpected value ("True" instead of "true") of my json boolean property.

    Is this an issue with Powershell? With their API back-end?

    Using the below version of Powershell

    Major  Minor  Build  Revision
    -----  -----  -----  --------
    5      1      16299  1004

    Code call in Powershell is as follows

    $properties = @{
    
      Headers = @{
    
        Authorization = 'Bearer ' + $token
    
        'Content-Type' = 'application/json'
    
      }
      Method = 'Get'
      Uri = $uri
    }
    $result = Invoke-RestMethod @properties



    -DG-

    Monday, June 17, 2019 1:13 PM

All replies

  • This is not an issue.  The values will be converted correctly for Booleans no matter what the case it is in.  When manipulating Json we usually convert to an object and manipulate the object.

    Json is not case sensitive.  Windows and PowerShell are not case sensitive.

    Example:

    $TRUE
    $true
    $FALSE
    $false

    PS D:\scripts> @{Boolean = $true} | ConvertTo-Json
    {
        "Boolean":  true
    }
    PS D:\scripts> @{Boolean = $TRUE} | ConvertTo-Json
    {
        "Boolean":  true
    }
    PS D:\scripts> $json = @{Boolean = $TRUE} | ConvertTo-Json
    PS D:\scripts> $json | ConvertFrom-Json
    
    Boolean
    -------
       True
    
    PS D:\scripts> $json
    {
        "Boolean":  true
    }
    PS D:\scripts> $json -replace 'true', 'TRUE'
    {
        "Boolean":  TRUE
    }
    PS D:\scripts> $json | ConvertFrom-Json
    
    Boolean
    -------
       True
    
    
    PS D:\scripts>


    \_(ツ)_/

    Monday, June 17, 2019 1:35 PM
    Moderator
  • Also note that JSON is plain text.  Invoke-RestMethod does not convert this returned string.  If the API return the string incorrectly then you need to contact the owner of the site.

    If you still think this is a bug then this is not a forum for bug reports.  Please post bugs and requests on the "PowerShell UserVoice" site.


    \_(ツ)_/

    Monday, June 17, 2019 1:45 PM
    Moderator
  • If the REST API simply returns pure JSON, how is powershell aware that this is a boolean value?

    The request and response for curl, for instance, are below. The value is returned in lowercase.

    curl -H "Authorization: Bearer TOKEN_TOKEN" "URI_URI"

    RESPONSE:

    {"id":"xxx-xxx-xxx-xxx","targetName":"first.last","properties":{"Text-Uppercase":"True","TestCheckbox":true,"Text-Lowercase":"lowercase"}}

    I'll try to report a bug on uservoice.


    -DG-

    Monday, June 17, 2019 5:28 PM
  • It  doesn't know.  What is returned is a Json string that has to be converted to an object.  The converter knows how t convert the strings to  Booleans.

    help convert*-Json -full


    \_(ツ)_/

    Monday, June 17, 2019 7:25 PM
    Moderator
  • Thanks jrv - I confirmed with the vendor that the API returns simple text. I guess the implication is that anytime Invoke-restmethod retrieves a json string value with the strings "TRUE" or "true" or "True", as part of the json deserialization it will translate it to a boolean.

    Thank you,


    -DG-

    Monday, June 17, 2019 10:33 PM
  • There is no Json serialization.  It is just plain text just like JavaScript.  All web scripting must be plain text.  Json is just a method of using txt to describe an object.  This text can be stored or transmitted to and from a web page.  Web pages only manage text. 

    True and False in and case and not in quotes are always seen as Booleans.  Just like a number not in quotes is seen as a number.


    \_(ツ)_/

    Monday, June 17, 2019 10:42 PM
    Moderator