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
}
]
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
Let's say I have a table with rows like number, image. But image is not mandatory, It can be null and when I'm selecting I want to prioritize the row with image over the one with null so i could get clean array with only one row per number.
SELECT DISTINCT number, image FROM table ORDER BY number ASC
What's now with SELECT DISTINCT:
[
{
"number": 1,
"image": null
},
{
"number": 1,
"image": "https://example.com/image1.png"
},
{
"number": 2,
"image": null
},
{
"number": 2,
"image": "https://example.com/image2.png"
},
{
"number": 3,
"image": "https://example.com/image3.png"
},
{
"number": 3,
"image": null
},
{
"number": 4,
"image": null
}
]
What I want to get:
[
{
"number": 1,
"image": "https://example.com/image1.png"
},
{
"number": 2,
"image": "https://example.com/image2.png"
},
{
"number": 3,
"image": "https://example.com/image3.png"
},
{
"number": 4,
"image": null
}
]
Try this:
SELECT number, image FROM table ORDER BY ISNULL(LEFT(image,0));
We are tying to format a json similar to this:
[
{"id": 1,
"type": "A",
"changes": [
{"id": 12},
{"id": 13}
],
"wanted_key": "good",
"unwanted_key": "aaa"
},
{"id": 2,
"type": "A",
"unwanted_key": "aaa"
},
{"id": 3,
"type": "B",
"changes": [
{"id": 31},
{"id": 32}
],
"unwanted_key": "aaa",
"unwanted_key2": "aaa"
},
{"id": 4,
"type": "B",
"unwanted_key3": "aaa"
},
null,
null,
{"id": 7}
]
into something like this:
[
{
"id": 1,
"type": "A",
"wanted_key": true # every record must have this key/value
},
{
"id": 12, # note: this was in the "changes" property of record id 1
"type": "A", # type should be the same type than record id 1
"wanted_key": true
},
{
"id": 13, # note: this was in the "changes" property of record id 1
"type": "A", # type should be the same type than record id 1
"wanted_key": true
},
{
"id": 2,
"type": "A",
"wanted_key": true
},
{
"id": 3,
"type": "B",
"wanted_key": true
},
{
"id": 31, # note: this was in the "changes" property of record id 3
"type": "B", # type should be the same type than record id 3
"wanted_key": true
},
{
"id": 32, # note: this was in the "changes" property of record id 3
"type": "B", # type should be the same type than record id 3
"wanted_key": true
},
{
"id": 4,
"type": "B",
"wanted_key": true
},
{
"id": 7,
"type": "UNKN", # records without a type should have this type
"wanted_key": true
}
]
So far, I've been able to:
remove null records
obtain the keys we need with their default
give records without a type a default type
What we are missing:
from records having a changes key, create new records with the type of their parent record
join all records in a single array
Unfortunately we are not entirely sure how to proceed... Any help would be appreciated.
So far our jq goes like this:
del(..|nulls) | map({id, type: (.type // "UNKN"), wanted_key: (true)}) | del(..|nulls)
Here's our test code:
https://jqplay.org/s/eLAWwP1ha8P
The following should work:
map(select(values))
| map(., .type as $type | (.changes[]? + {$type}))
| map({id, type: (.type // "UNKN"), wanted_key: true})
Only select non-null values
Return the original items followed by their inner changes array (+ outer type)
Extract 3 properties for output
Multiple map calls can usually be combined, so this becomes:
map(
select(values)
| ., (.type as $type | (.changes[]? + {$type}))
| {id, type: (.type // "UNKN"), wanted_key: true}
)
Another option without variables:
map(
select(values)
| ., .changes[]? + {type}
| {id, type: (.type // "UNKN"), wanted_key: true}
)
# or:
map(select(values))
| map(., .changes[]? + {type})
| map({id, type: (.type // "UNKN"), wanted_key: true})
or even with a separate normalization step for the unknown type:
map(select(values))
| map(.type //= "UNKN")
| map(., .changes[]? + {type})
| map({id, type, wanted_key: true})
# condensed to a single line:
map(select(values) | .type //= "UNKN" | ., .changes[]? + {type} | {id, type, wanted_key: true})
Explanation:
Select only non-null values from the array
If type is not set, create the property with value "UNKN"
Produce the original array items, followed by their nested changes elements extended with the parent type
Reshape objects to only contain properties id, type, and wanted_key.
Here's one way:
map(
select(values)
| (.type // "UNKN") as $type
| ., .changes[]?
| {id, $type, wanted_key: true}
)
[
{
"id": 1,
"type": "A",
"wanted_key": true
},
{
"id": 12,
"type": "A",
"wanted_key": true
},
{
"id": 13,
"type": "A",
"wanted_key": true
},
{
"id": 2,
"type": "A",
"wanted_key": true
},
{
"id": 3,
"type": "B",
"wanted_key": true
},
{
"id": 31,
"type": "B",
"wanted_key": true
},
{
"id": 32,
"type": "B",
"wanted_key": true
},
{
"id": 4,
"type": "B",
"wanted_key": true
},
{
"id": 7,
"type": "UNKN",
"wanted_key": true
}
]
Demo
Something like below should work
map(
select(type == "object") |
( {id}, {id : ( .changes[]? .id )} ) +
{ type: (.type // "UNKN"), wanted_key: true }
)
jq play - demo
I have a table of cart with 2 columns (user_num, data).
user_num will have the phone number of user and
data will have an array of object like [{ "id": 1, "quantity": 1 }, { "id": 2, "quantity": 2 }, { "id": 3, "quantity": 3 }] here id is product id.
user_num | data
----------+--------------------------------------------------------------------------------------
1 | [{ "id": 1, "quantity": 1 }, { "id": 2, "quantity": 2 }, { "id": 3, "quantity": 3 }]
I want to add more data of products in above array of objects in PostgreSQL.
Thanks!
To add the value use the JSONB array append operator ||
Demo
update
test
set
data = data || '[{"id": 4, "quantity": 4}, {"id": 5, "quantity": 5}]'
where
user_num = 1;
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
}
]
}
]