One of the columns in my table, has JSONArray data. I have used jsonb_agg() in a jsonb column to a view. Now the data from view looks like below
[
{
"Android": 2,
"Windows": 1,
"Macintosh": 1
},
{
"iOS": 1,
"Android": 2,
"Windows": 2,
"Macintosh": 2
},
{},
{
"Android": 1,
"Windows": 1
},
{
"Android": 1
},
{
"iOS": 1,
"Android": 2
},
{
"iOS": 2,
"Android": 1
},
{
"iOS": 2
},
{
"Android": 1
},
{
"iOS": 2,
"Windows": 1
},
{
"Android": 5
},
{},
{},
{
"iOS": 1,
"Android": 1
},
{},
{},
{
"Windows": 3
}
]
However, I need to product the below result
{
"Android": 16,
"Windows": 8,
"Macintosh": 3,
"iOS": 9
}
Is there a way within PostgreSQL to achieve this?
Yes there is a way. Here it is.
select to_jsonb(t.*) from
(
select
sum((j->>'Android')::numeric) "Android",
sum((j->>'Windows')::numeric) "Windows",
sum((j->>'Macintosh')::numeric) "Macintosh",
sum((j->>'iOS')::numeric) "iOS"
from jsonb_array_elements('[{"Android": 2, "Windows": 1, "Macintosh": 1}, {"iOS": 1, "Android": 2, "Windows": 2, "Macintosh": 2}, {}, {"Android": 1, "Windows": 1}, {"Android": 1}, {"iOS": 1, "Android": 2}, {"iOS": 2, "Android": 1}, {"iOS": 2}, {"Android": 1}, {"iOS": 2, "Windows": 1}, {"Android": 5}, {}, {}, {"iOS": 1, "Android": 1}, {}, {}, "Windows": 3}]'::jsonb) as j
) as t;
As a parameterized query:
select to_jsonb(t.*) from
(
select
sum((j->>'Android')::numeric) "Android",
sum((j->>'Windows')::numeric) "Windows",
sum((j->>'Macintosh')::numeric) "Macintosh",
sum((j->>'iOS')::numeric) "iOS"
from jsonb_array_elements(?::jsonb) as j
) as t;
You can use dynamic key, value like that:
with data as (
select
je.key,
sum(je.value::int)
from
json_array_elements('[{"Android": 2, "Windows": 1, "Macintosh": 1}, {"iOS": 1, "Android": 2, "Windows": 2, "Macintosh": 2}, {}, {"Android": 1, "Windows": 1}, {"Android": 1}, {"iOS": 1, "Android": 2}, {"iOS": 2, "Android": 1}, {"iOS": 2}, {"Android": 1}, {"iOS": 2, "Windows": 1}, {"Android": 5}, {}, {}, {"iOS": 1, "Android": 1}, {}, {}, {"Windows": 3}]') d
cross join json_each_text(d) as je
group by 1
)
select json_object_agg(d.key, d.sum)
from data d
Or if you have table and column you can use this sample format like that:
with data as (
select
je.key,
sum(je.value::int)
from
your_table t join
json_array_elements(t.your_json_coulmn) d on true
cross join json_each_text(d) as je
group by 1
)
select json_object_agg(d.key, d.sum)
from data d
You can combine many json/jsonb functions to get the result
SELECT
jsonb_agg(json_build_object(key, sum))
FROM (
SELECT
key,
sum(value)
FROM (
SELECT
(arr).key,
(arr).value::int
FROM (
SELECT
jsonb_each(j) arr
FROM (
SELECT
jsonb_array_elements('[{"Android": 2, "Windows": 1, "Macintosh": 1}, {"iOS": 1, "Android": 2, "Windows": 2, "Macintosh": 2}, {}, {"Android": 1, "Windows": 1}, {"Android": 1}, {"iOS": 1, "Android": 2}, {"iOS": 2, "Android": 1}, {"iOS": 2}, {"Android": 1}, {"iOS": 2, "Windows": 1}, {"Android": 5}, {}, {}, {"iOS": 1, "Android": 1}, {}, {}, {"Windows": 3}]
'::jsonb) AS j) AS jpart) AS x) sub
GROUP BY
1) AS sub2
output: [{"Windows": 8}, {"Android": 16}, {"iOS": 9}, {"Macintosh": 3}]
Related
I am trying to merge 2 JSON file Objects. But 2 of the objects are getting appended with the same value in both the JSON file.
Here Month & Year values are same in both JSON file like Month=1 & Year=2023, so its getting appended in the output JSON, how can i avoid this? The expected value in the output is Month=1 & Year=2023 and not Month=11 & Year=20232023.
Input1
{
"data": [
{
"item_value_01": "APPLICATION",
"quantity": 1
},
{
"item_value_01": "HERBERT",
"quantity": 1
}
],
"month": "1",
"year": "2023"
}
Input2
{
"data": [
{
"item_value_01": "Test",
"quantity": 1
},
{
"item_value_01": "Country",
"quantity": 1
}
],
"month": "1",
"year": "2023"
}
Wrong Output
{
"data": [
{
"item_value_01": "APPLICATION",
"quantity": 1
},
{
"item_value_01": "HERBERT",
"quantity": 1
},
{
"item_value_01": "Test",
"quantity": 1
},
{
"item_value_01": "Country",
"quantity": 1
}
],
"month": "11",
"year": "20232023"
}
Expected Output
{
"data": [
{
"item_value_01": "APPLICATION",
"quantity": 1
},
{
"item_value_01": "HERBERT",
"quantity": 1
},
{
"item_value_01": "Test",
"quantity": 1
},
{
"item_value_01": "Country",
"quantity": 1
}
],
"month": "1",
"year": "2023"
}
Playbook
- set_fact:
list: "{{ list|default({})|
combine({item: input1[item]|default([]) +
input2[item]|default([])}, recursive=true) }}"
loop: "{{ (input2.keys()|list + input1.keys()|list)|unique }}"
Set list_merge='append' in the filter combine
result: "{{ [input1, input2]|combine(list_merge='append') }}"
gives the expected result
result:
data:
- item_value_01: APPLICATION
quantity: 1
- item_value_01: HERBERT
quantity: 1
- item_value_01: Test
quantity: 1
- item_value_01: Country
quantity: 1
month: '1'
year: '2023'
Example of a complete playbook for testing
- hosts: localhost
vars:
input1:
data:
- item_value_01: APPLICATION
quantity: 1
- item_value_01: HERBERT
quantity: 1
month: '1'
year: '2023'
input2:
data:
- item_value_01: Test
quantity: 1
- item_value_01: Country
quantity: 1
month: '1'
year: '2023'
result: "{{ [input1, input2]|combine(list_merge='append') }}"
tasks:
- debug:
var: result
I have below data
against below query
declare #t table
(
Id int identity,
name varchar(50),
rootid int,
level int
);
insert into #t(name, rootid, level)
values
('Home', 0, 0)
,('Transaction', 0, 0)
, ('Settings', 0, 0)
,('Purchase Request', 2, 1)
,('Purchase Order', 2, 1)
,('Inventory', 2, 1)
,('Payment Advice', 2, 1)
,('Setup', 3, 1)
,('Budget', 3, 1)
,('CRC', 3, 1)
,('Create PR', 4, 3);
select * from #t;
Desire output:
[{
"Id": 1,
"name": "Home",
"rootid": 0,
"level": 0
}, {
"Id": 2,
"name": "Transaction",
"rootid": 0,
"level": 0,
"children": [{
"Id": 4,
"name": "Purchase Request",
"rootid": 2,
"level": 1,
"children": [{
"Id": 11,
"name": "Create PR",
"rootid": 4,
"level": 3
}]
}, {
"Id": 5,
"name": "Purchase Order",
"rootid": 2,
"level": 1
}, {
"Id": 6,
"name": "Inventory",
"rootid": 2,
"level": 1
}, {
"Id": 7,
"name": "Payment Advice",
"rootid": 2,
"level": 1
}]
}, {
"Id": 3,
"name": "Settings",
"rootid": 0,
"level": 0,
"children": [{
"Id": 8,
"name": "Setup",
"rootid": 3,
"level": 1
}, {
"Id": 9,
"name": "Budget",
"rootid": 3,
"level": 1
}, {
"Id": 10,
"name": "CRC",
"rootid": 3,
"level": 1
}]
}]
Also Tried #Iptr answer:
;WITH result (id, name, rootId, parent, Level) AS
(
SELECT id,
name,
RootId,
Id as Parent,
0 as Level
FROM #t
WHERE RootId= 0
UNION ALL
SELECT t.id,
t.Name,
t.RootId,
r.Parent,
r.Level + 1
FROM #t t
INNER JOIN result r ON r.id = t.RootId
)
SELECT t.*, json_query(nullif(c.children, '[{}]')) as children
FROM #t as t
outer apply (
select
(
select r.*
from result as r
where r.parent = t.Id
and r.level > 0
order by r.id
for json auto
) as children
) as c
where t.level = 0
order by t.Level
for json auto;
Output
[{
"Id": 1,
"name": "Home",
"rootid": 0,
"level": 0
}, {
"Id": 2,
"name": "Transaction",
"rootid": 0,
"level": 0,
"children": [{
"id": 4,
"name": "Purchase Request",
"rootId": 2,
"parent": 2,
"Level": 1
}, {
"id": 5,
"name": "Purchase Order",
"rootId": 2,
"parent": 2,
"Level": 1
}, {
"id": 6,
"name": "Inventory",
"rootId": 2,
"parent": 2,
"Level": 1
}, {
"id": 7,
"name": "Payment Advice",
"rootId": 2,
"parent": 2,
"Level": 1
}, {
"id": 11,
"name": "Create PR",
"rootId": 4,
"parent": 2,
"Level": 2
}]
}, {
"Id": 3,
"name": "Settings",
"rootid": 0,
"level": 0,
"children": [{
"id": 8,
"name": "Setup",
"rootId": 3,
"parent": 3,
"Level": 1
}, {
"id": 9,
"name": "Budget",
"rootId": 3,
"parent": 3,
"Level": 1
}, {
"id": 10,
"name": "CRC",
"rootId": 3,
"parent": 3,
"Level": 1
}]
}]
Above query is not returning nth json child objects, let say if I have nth level of menu items, Parent have multiple Childs and Childs have multiple Childs like treeview.
Tried #Naveen Arora answer:
select ID,name,'' as id,'' as name from Navigations where id not in (select rootid from Navigations) and rootid=0
union
select B.id,B.name,A.id,A.name from Navigations A join Navigations B on A.rootid=B.id
FOR JSON AUTO;
But output
[{
"ID": 1,
"name": "Home",
"id": 0,
"name": ""
}, {
"ID": 2,
"name": "Transaction",
"id": 4,
"name": "Create PR"
}, {
"ID": 2,
"name": "Transaction",
"id": 5,
"name": "Generate PO"
}, {
"ID": 2,
"name": "Transaction",
"id": 6,
"name": "Create Receipt"
}, {
"ID": 2,
"name": "Transaction",
"id": 7,
"name": "Create Issue Request"
}, {
"ID": 2,
"name": "Transaction",
"id": 8,
"name": "Create Issue Note"
}, {
"ID": 2,
"name": "Transaction",
"id": 9,
"name": "Approve Payment Advice"
}, {
"ID": 3,
"name": "Settings",
"id": 11,
"name": "Navigation Management"
}, {
"ID": 11,
"name": "Navigation Management",
"id": 12,
"name": "Navigation & Form Mapping"
}]
Above output it's not include Childs node. Like in Settings I have Navigation Management -> Navigation & Form Mapping
If the sql server version is 2016 or newer than 2016 then you can use FOR JSON PATH.
Assuming that results are stored in test table. This is just to give you an idea how you can do this, may not give you the exact output but you can change it as per your requirement.
SELECT
t.Id AS 'Id',
t.Name AS 'Name',
children = (
SELECT A.id,A.name from test A join test B on A.rootid=B.id
FOR JSON PATH
)
FROM Test t
FOR JSON PATH;
And if it is older than 2016 then you may refer this.
declare #t table
(
Id int identity,
name varchar(50),
rootid int,
level int
);
insert into #t(name, rootid, level)
values
('Home', 0, 0),('Transaction', 0, 0), ('Settings', 0, 0),
('Create PR', 2, 1), ('Generate PO', 2, 1), ('Create Receipt', 2, 1), ('Create Issue Request', 2, 1), ('Create Issue Note', 2, 1), ('Approve Payment Advice', 2, 1),
('Navigation Management', 3, 1), ('Navigation & Form Mapping', 3, 1);
select * from #t;
;WITH result (id, name, rootId, parent, Level) AS
(
SELECT id,
name,
RootId,
Id as Parent,
0 as Level
FROM #t
WHERE RootId= 0
UNION ALL
SELECT t.id,
t.Name,
t.RootId,
r.Parent,
r.Level + 1
FROM #t t
INNER JOIN result r ON r.id = t.RootId
)
SELECT t.*, json_query(nullif(c.children, '[{}]')) as children
FROM #t as t
outer apply (
select
(
select r.*
from result as r
where r.parent = t.Id
and r.level > 0
order by r.id
for json auto
) as children
) as c
where t.level = 0
order by t.Level
for json auto;
SELECT t.*, json_query(nullif(c.children, '[{}]')) as children
FROM #t as t
outer apply (
select
(
select r.*
from #t as r
where r.rootid = t.Id
and r.level > 0
order by r.id
for json auto
) as children
) as c
where t.level = 0
order by t.Level
for json auto;
My apology for late posting my answer. But I really appreciate the efforts of #Iptr and #NaveenArora answer on my post. After I do some brain storming on my case I've finally found the way to do it.
Create this function:
create function [dbo].[fnUDFCreateJSON](#currentId int)
returns varchar(max)
begin
declare #json nvarchar(max)
IF #currentId <> 0
BEGIN
set #json =
(
select [ID], [Name], CSSClass, RouteURL, json_query(dbo.fnUDFCreateJSON([ID])) as SubNavigation
from dbo.Navigations
where RootId = #currentId
for json auto
);
END
ELSE
BEGIN
set #json =
(
select [ID], [Name], CSSClass, RouteURL, '' as SubNavigation from dbo.Navigations where RootId = 0
for json auto
);
END
return #json
end
and call it by using stored procedure:
CREATE PROCEDURE [dbo].[spGetStartupNavigations]
AS
BEGIN
SELECT
(SELECT
ID, Name, CSSClass, RouteURL,
JSON_QUERY (dbo.fnUDFCreateJSON(ID)) AS SubNavigation
FROM
dbo.Navigations
WHERE
RootId = 0
FOR JSON AUTO) AS Navigation
END
That's it.
My database is as follows:
id | name | parentId
1 | Cate1 |null
2 | Cate2 | 1
3 | Cate3 | 2
My expected output is
{
id:1,
name:"Cate1",
parentId:null,
subCate:[
{
id:2,
name:"Cate2",
parentId:1,
subCate:[
{
id:3,
name:"Cate3",
parentId:2,
subCate:[]
}
]
}
]
}
And so on ...
So how to get this result.
function getNestedChildren(arr, parentId) {
var out = []
for (var i in arr) {
if (arr[i].parentId == parentId) {
var children = getNestedChildren(arr, arr[i].id)
if (children.length) {
arr[i].subCate = children
}
out.push(arr[i])
}
}
return out
}
console.log(JSON.stringify(getNestedChildren([
{id: 1, name: 'edwd', parentId: null},
{id: 2, name: 'ttt', parentId: null},
{id: 3, name: 'ooo', parentId: 1},
{id: 4, name: 'ppp', parentId: 3},
{id: 5, name: 'lll', parentId: 4},
{id: 6, name: 'mmm', parentId: 4},
{id: 7, name: 'nnn', parentId: 3},
{id: 8, name: 'zzz', parentId: 2}
], null)))
This outputs:
[
{
"id": 1,
"name": "edwd",
"parentId": null,
"subCate": [
{
"id": 3,
"name": "ooo",
"parentId": 1,
"subCate": [
{
"id": 4,
"name": "ppp",
"parentId": 3,
"subCate": [
{
"id": 5,
"name": "lll",
"parentId": 4
},
{
"id": 6,
"name": "mmm",
"parentId": 4
}
]
},
{
"id": 7,
"name": "nnn",
"parentId": 3
}
]
}
]
},
{
"id": 2,
"name": "ttt",
"parentId": null,
"subCate": [
{
"id": 8,
"name": "zzz",
"parentId": 2
}
]
}
]
Sorry for the basic of this question, I just cannot wrap my head around this one.
I need the output from SQL Server to look like this.
In a little more human readable format:
var data = [
{
name: '2017', id: -1,
children: [
{ name: '01-2017', id: 11 },
{ name: '02-2017', id: 12 },
{ name: '03-2017', id: 13 },
{ name: '04-2017', id: 14 },
{ name: '05-2017', id: 15 },
]
},
{
name: '2018', id: -1,
children: [
{ name: '01-2018', id: 6 },
{ name: '02-2018', id: 7 },
{ name: '03-2018', id: 8 },
{ name: '04-2018', id: 9 },
{ name: '05-2018', id: 10 },
]
}
];
This is a snapshot of the data:
The group I will be working with is userid = 1.
My first thought was to use a cursor to loop through all the distinct reportYear for userid = 1, then a select based on the year and the userid to fill in the sub-query.
There has to be a way without using a cursor.
You can achieve the desired output joining your table to a query that extracts all the years to be used at the top level elements and then generating the json using FOR JSON AUTO:
declare #tmp table (monthlyReportID int, userID int, reportMonth int, reportYear int)
insert into #tmp values
( 6, 1, 1, 2018),
( 7, 1, 2, 2018),
( 8, 1, 3, 2018),
( 9, 1, 4, 2018),
(10, 1, 5, 2018),
(11, 1, 1, 2017),
(12, 1, 2, 2017),
(13, 1, 3, 2017),
(14, 1, 4, 2017),
(15, 1, 5, 2017)
select years.[name], children.[name], children.[id] from
(
select distinct reportYear as [name] from #tmp
) as years
left join
(
select monthlyReportID as [id]
,right('0' + cast(reportMonth as varchar(2)),2) + '-' + cast(reportYear as varchar(4)) as [name]
,reportYear as [year]
from #tmp
) as children
on children.[Year] = years.[name]
for json auto
I omitted the ID field because in your desired output it is always set to -1 and I was not able to understand the logic behind it.
Nonetheless you should be able to easily edit the script above to obtain the value you need.
Here are the results:
[
{
"name": 2017,
"children": [
{"name": "01-2017", "id": 11},
{"name": "02-2017", "id": 12},
{"name": "03-2017", "id": 13},
{"name": "04-2017", "id": 14},
{"name": "05-2017", "id": 15}
]
},
{
"name": 2018,
"children": [
{"name": "01-2018", "id": 6},
{"name": "02-2018", "id": 7},
{"name": "03-2018", "id": 8},
{"name": "04-2018", "id": 9},
{"name": "05-2018", "id": 10}
]
}
]
how to correctly convert a Powershell array of object properties to a json array of values, that is without the objects property lables in the array.
for example:
I want to make 2 json arrays for chart.js
I will group some process objects:
$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
}
]
how do I omit the "Count" label so PowerShell creates a single json array of the values only, as in the process name array.
I have tried
$chartvalues = $processgroup.count
results in the count of the number of groups
and I have tried
$chartvalues = $ processgroup | select count -expandproperty count | convertto-json
with the same result as above example
You want a list, rather than object with key/value pair, so this should work*:
$groups| foreach-object {$_.count}|convertto-json
When you do select after the pipeline it creates an object, thus pushes the object property name as well as the value as key/value pair into the json conversion process.
i.e. If you have used select for name property, you would get the name/value pair in the json too...
$groups| select-object name| convertto-json
*may be better solutions out there... but itworks..
Silly me - I only needed to select and expand the property. like so.
$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
]
All you have to do use the -ExpandProperty property of Select-Object to achieve your stated desire. Is there any reason not to preserve the property->value notation which association inside of a single JSON array? e.g.:
$processgroup = get-process | group -property name,count | select name,count | convertto-json
yields:
$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
}
]