没有标签的Powershell convertto-json

时间:2017-06-08 11:53:29

标签: json powershell powershell-v3.0

如何正确地将Powershell对象属性数组转换为json数组值,即数组中没有对象属性标签。

例如: 我想为chart.js制作2个json数组 我将对一些过程对象进行分组:

$processgroup = get-process | group -property name

$processgroup.gettype()

IsPublic IsSerial Name                                     BaseType                                                                                 
-------- -------- ----                                     --------                                                                                 
True     True     Object[]                                 System.Array  

$chartlabels = $processgroup.name | convertto-json

$chartlabels

$chartlabels
[
    "ApMsgFwd",
    "ApntEx",
    "Apoint",
    "ApplicationFrameHost",
    "armsvc",
    "BtwRSupportService",
    "chrome",
    "com.docker.proxy",
    "com.docker.service",
    "concentr",
    "conhost",
    "csrss",
    "dllhost",
    "Docker for Windows",
    "dockerd",
    "dwm",
    "Everything",
    "EXCEL",
    "explorer",
    "fontdrvhost",
    "GROOVE",
    "hidfind",
    "HidMonitorSvc",
    "Idle",
    "iexplore",
    "IpOverUsbSvc",
    "jucheck",
    "jusched",
    "LicensingUI",
    "lsass",
    "mDNSResponder",
    "Memory Compression",
    "mqsvc",
    "MSASCuiL",
    "MsMpEng",
    "MSOIDSVC",
    "MSOIDSVCM",
    "MySQLNotifier",
    "NisSrv",
    "notepad",
    "notepad++",
    "nvSCPAPISvr",
    "nvvsvc",
    "nvwmi64",
    "nvxdsync",
    "OfficeClickToRun",
    "OneDrive",
    "OUTLOOK",
    "powershell",
    "powershell_ise",
    "prevhost",
    "Receiver",
    "redirector",
    "rundll32",
    "RuntimeBroker",
    "SearchIndexer",
    "SearchUI",
    "Secure System",
    "SecurityHealthService",
    "SelfServicePlugin",
    "services",
    "SettingSyncHost",
    "ShellExperienceHost",
    "sihost",
    "SkypeHost",
    "smss",
    "SMSvcHost",
    "spiceworks",
    "spiceworks-httpd",
    "spoolsv",
    "SppExtComObj",
    "sppsvc",
    "sqlwriter",
    "svchost",
    "System",
    "SystemSettings",
    "taskhostw",
    "TSVNCache",
    "vmcompute",
    "vmms",
    "vmnat",
    "vmnetdhcp",
    "vmware-authd",
    "vmware-tray",
    "vmware-usbarbitrator64",
    "wfcrun32",
    "wininit",
    "winlogon",
    "WINWORD",
    "WmiPrvSE",
    "WUDFHost"
]

#this is the array I want for charts labels, now for the chart value array

$chartvalues = $processgroup | select count | convertto-json

$chartvalues
[
    {
        "Count":  1
    },
    {
        "Count":  1
    },
    {
        "Count":  1
    },
    {
        "Count":  1
    },
    {
        "Count":  1
    },
    {
        "Count":  1
    },
    {
        "Count":  30
    },
    {
        "Count":  1
    },
    {
        "Count":  1
    },
    {
        "Count":  1
    },
    {
        "Count":  5
    },
    {
        "Count":  2
    },
    {
        "Count":  1
    },
    {
        "Count":  1
    },
    {
        "Count":  1
    },
    {
        "Count":  1
    },
    {
        "Count":  2
    },
    {
        "Count":  1
    },
    {
        "Count":  1
    },
    {
        "Count":  2
    },
    {
        "Count":  1
    },
    {
        "Count":  1
    },
    {
        "Count":  1
    },
    {
        "Count":  1
    },
    {
        "Count":  4
    },
    {
        "Count":  1
    },
    {
        "Count":  1
    },
    {
        "Count":  1
    },
    {
        "Count":  30
    },
    {
        "Count":  1
    },
    {
        "Count":  1
    },
    {
        "Count":  1
    },
    {
        "Count":  1
    },
    {
        "Count":  1
    },
    {
        "Count":  1
    },
    {
        "Count":  1
    },
    {
        "Count":  1
    },
    {
        "Count":  1
    },
    {
        "Count":  1
    },
    {
        "Count":  2
    },
    {
        "Count":  1
    },
    {
        "Count":  1
    },
    {
        "Count":  2
    },
    {
        "Count":  2
    },
    {
        "Count":  1
    },
    {
        "Count":  1
    },
    {
        "Count":  1
    },
    {
        "Count":  1
    },
    {
        "Count":  1
    },
    {
        "Count":  2
    },
    {
        "Count":  1
    },
    {
        "Count":  1
    },
    {
        "Count":  1
    },
    {
        "Count":  2
    },
    {
        "Count":  1
    },
    {
        "Count":  1
    },
    {
        "Count":  1
    },
    {
        "Count":  1
    },
    {
        "Count":  1
    },
    {
        "Count":  1
    },
    {
        "Count":  1
    },
    {
        "Count":  1
    },
    {
        "Count":  1
    },
    {
        "Count":  1
    },
    {
        "Count":  1
    },
    {
        "Count":  1
    },
    {
        "Count":  2
    },
    {
        "Count":  1
    },
    {
        "Count":  2
    },
    {
        "Count":  1
    },
    {
        "Count":  1
    },
    {
        "Count":  1
    },
    {
        "Count":  1
    },
    {
        "Count":  75
    },
    {
        "Count":  1
    },
    {
        "Count":  1
    },
    {
        "Count":  1
    },
    {
        "Count":  2
    },
    {
        "Count":  1
    },
    {
        "Count":  1
    },
    {
        "Count":  1
    },
    {
        "Count":  1
    },
    {
        "Count":  1
    },
    {
        "Count":  1
    },
    {
        "Count":  1
    },
    {
        "Count":  1
    },
    {
        "Count":  1
    },
    {
        "Count":  1
    },
    {
        "Count":  2
    },
    {
        "Count":  1
    },
    {
        "Count":  1
    }
]

如何省略“Count”标签,以便PowerShell仅创建值的单个json数组,如进程名称数组中那样。

我试过了

$chartvalues = $processgroup.count

导致组数的计数

我试过

$chartvalues = $ processgroup | select count -expandproperty count | convertto-json

与上例

的结果相同

3 个答案:

答案 0 :(得分:1)

你想要一个列表,而不是带有键/值对的对象,所以这应该工作*:

 $groups| foreach-object {$_.count}|convertto-json

当您在管道之后执行选择时,它会创建一个对象,从而将对象属性名称以及值作为键/值对推送到json转换过程中。 即如果你使用了select for name属性,你也会在json中获得名称/值对...

 $groups| select-object name| convertto-json

*可能是更好的解决方案......但是itworks ..

答案 1 :(得分:1)

愚蠢的我 - 我只需要选择并扩展该属性。像这样。

$chartvalues = $processgroup | select -expandproperty count | convertto-json

$chartvalues
[
    1,
    1,
    1,
    1,
    1,
    1,
    30,
    1,
    1,
    1,
    5,
    2,
    1,
    1,
    1,
    1,
    2,
    1,
    1,
    2,
    1,
    1,
    1,
    1,
    4,
    1,
    1,
    1,
    30,
    1,
    1,
    1,
    1,
    1,
    1,
    1,
    1,
    1,
    1,
    2,
    1,
    1,
    2,
    2,
    1,
    1,
    1,
    1,
    1,
    2,
    1,
    1,
    1,
    2,
    1,
    1,
    1,
    1,
    1,
    1,
    1,
    1,
    1,
    1,
    1,
    1,
    2,
    1,
    2,
    1,
    1,
    1,
    1,
    75,
    1,
    1,
    1,
    2,
    1,
    1,
    1,
    1,
    1,
    1,
    1,
    1,
    1,
    1,
    2,
    1,
    1
]

答案 2 :(得分:-1)

您所要做的就是使用Select-Object的-ExpandProperty属性来实现您所期望的愿望。是否有任何理由不保留property->值表示法单个JSON数组中的哪个关联? e.g:

$processgroup = get-process | group -property name,count | select name,count | convertto-json

的产率:

$processgroup
[
    {
        "Name":  "acevents",
        "Count":  1
    },
    {
        "Name":  "acrotray",
        "Count":  1
    },
    {
        "Name":  "AGSService",
        "Count":  1
    },
    {
        "Name":  "aiCOMMAPI",
        "Count":  1
    },
    {
        "Name":  "armsvc",
        "Count":  1
    },
    {
        "Name":  "audiodg",
        "Count":  1
    },
    {
        "Name":  "AuditManagerService",
        "Count":  1
    },
    {
        "Name":  "CcmExec",
        "Count":  1
    },
    {
        "Name":  "chrome",
        "Count":  9
    },
    {
        "Name":  "conhost",
        "Count":  2
    },
    {
        "Name":  "csrss",
        "Count":  2
    },
    {
        "Name":  "dllhost",
        "Count":  2
    },
    {
        "Name":  "dwm",
        "Count":  1
    },
    {
        "Name":  "explorer",
        "Count":  1
    },
    {
        "Name":  "Idle",
        "Count":  1
    },
    {
        "Name":  "lsass",
        "Count":  1
    },
    {
        "Name":  "lync",
        "Count":  1
    },
    {
        "Name":  "msdtc",
        "Count":  1
    }
]