In Windows Powershell I have a simple Invoke-SqlCmd
query which returns the following table:
ID Text
-- ----
1 FOO
2 BAR
3 SPAM
Piping it to | ConvertTo-Json -Depth 1
yields the following result:
[
{
"RowError": "",
"RowState": 1,
"Table": "",
"ItemArray": "1 FOO",
"HasErrors": false,
"ID": 1,
"Text": "FOO"
},
{
"RowError": "",
"RowState": 1,
"Table": "",
"ItemArray": "2 BAR",
"HasErrors": false,
"ID": 2,
"Text": "BAR"
},
{
"RowError": "",
"RowState": 1,
"Table": "",
"ItemArray": "3 SPAM",
"HasErrors": false,
"ID": 3,
"Text": "SPAM"
}
]
My desired output would be not this bare array, but an object having a single property having "Products" as name and the array as value.
Besides, I'd like my array entries being objecs having just the columns of the SQL table as properties.
That is, my desired output would be:
{
"Products": [
{
"ID": 1,
"Text": "FOO"
},
{
"ID": 2,
"Text": "BAR"
},
{
"ID": 3,
"Text": "SPAM"
}
]
}
How can I achieve it?
EDIT: The query and its result from SQL Server Management Studio are the following: