Matt
Matt

Reputation: 149

Powershell ConvertTo-Json keys in lowercase

Running this code

get-service | Select-Object -Property displayname, status, name | convertTo-Json

results for instance in an output like this:

{
    "DisplayName":  "Adobe Acrobat Update Service",
    "Status":  4,
    "Name":  "AdobeARMservice"
},
{
    "DisplayName":  "Adobe Flash Player Update Service",
    "Status":  1,
    "Name":  "AdobeFlashPlayerUpdateSvc"
},

Is it possible to return the keys in lowercase?

Upvotes: 1

Views: 1023

Answers (2)

4c74356b41
4c74356b41

Reputation: 72171

You could use calculated properties:

get-service | Select-Object -Property @{n='displayname';e={$_.displayname.tolower()}}, status, name | convertTo-Json

what this does is changes the way output is formatted

ps. you could repeat that with all properties pps. https://blogs.technet.microsoft.com/josebda/2014/04/19/powershell-tips-for-building-objects-with-custom-properties-and-special-formatting/

Upvotes: 6

Victor Silva
Victor Silva

Reputation: 780

You can use Regular Expressions to solve the requirement:

#Create a variable with the info
$json = Get-Service | Select-Object -Property displayname, status, name | convertTo-Json
#Use RegEx
[regex]::Replace(
    $json,
    '(?<=")(\w+)(?=":)',
    {$args[0].Groups[1].Value.ToLower()}
)

And the output is like this:

[
    {
        "displayname":  "Application Identity",
        "status":  1,
        "name":  "AppIDSvc"
    },
    {
        "displayname":  "Application Information",
        "status":  4,
        "name":  "Appinfo"
    },

Upvotes: 2

Related Questions