Powershell array with single hashtable - powershell

I am trying to build a Powershell array containing a single hashtable.
$params = #{
"name" = "bob"
"age" = "30"
}
$params | ConvertTo-Json
current output:
{
"age": "30",
"name": "bob"
}
Desired output:
[
{
"age": "30",
"name": "bob"
}
]

Try it this way:
$params = #{
"name" = "bob"
"age" = "30"
}
ConvertTo-Json #($params)
The #() syntax makes it an array.
You have to give the ConvertTo-Json cmdlet the input as a parameter, because the pipeline will automatically "unroll" the array and you'll be right back where you started.

Related

Powershell add column name to array data then convert it to JSON

Basically I formed an array of data based on certain conditions inside a loop and arrary data is something like this:
student1 RL123 S12 student2 RL423 S32 student6 RL166 S02 student34 RL993 P12 student99 RL923 S12
Above array data needs to be converted to JSON as below:
{
"Name" : "student1"
"RollNo" : "RL123"
"SubjectID" : "S12"
},
{
"Name" : "student2"
"RollNo" : "RL423"
"SubjectID" : "S32"
},
{
"Name" : "student6"
"RollNo" : "RL166"
"SubjectID" : "S02"
},
{
"Name" : "student34"
"RollNo" : "RL993"
"SubjectID" : "RL993"
},
{
"Name" : "student99"
"RollNo" : "RL923"
"SubjectID" : "S12"
}
If indeed your array is an array of strings (let's call it $students), you can skip the below line.
However, the way you have formatted it in your question makes it a string with space-separated items, so if that is the case, we should create a proper array from it by splitting on the whitespaces:
$students = 'student1 RL123 S12 student2 RL423 S32 student6 RL166 S02 student34 RL993 P12 student99 RL923 S12' -split '\s+'
Now just iterate over the values in the array and create objects using 3 array elements on each object:
$data = for ($i = 0; $i -lt $students.Count; $i+=3) {
[PsCustomObject]#{
Name = $students[$i]
RollNo = $students[$i + 1]
SubjectID = $students[$i + 2]
}
}
# here you convert the object array to JSON
$data | ConvertTo-Json
Output:
[
{
"Name": "student1",
"RollNo": "RL123",
"SubjectID": "S12"
},
{
"Name": "student2",
"RollNo": "RL423",
"SubjectID": "S32"
},
{
"Name": "student6",
"RollNo": "RL166",
"SubjectID": "S02"
},
{
"Name": "student34",
"RollNo": "RL993",
"SubjectID": "P12"
},
{
"Name": "student99",
"RollNo": "RL923",
"SubjectID": "S12"
}
]
the opening [ and closing ] denote that we're dealing with an array in Json syntax

How to delete entries when a specific object property does not exists in Powershell?

For example, in the following PSCustomObject, I wish to remove the objects for which TagName does not exist
$dataset1 = #(
#{
MachineName = "AAA"
ID = "111"
TagName = "GroupA"
},
#{
MachineName = "BBB"
ID = "222"
TagName = "GroupB"
},
#{
MachineName = "CCC"
ID = "111"
TagName = ""
},
#{
MachineName = "DDD"
ID = "333"
TagName = ""
},
#{
MachineName = "EEE"
ID = "111"
TagName = ""
}
}
So, after deletion the $dataset1 should contain the following:
$dataset1 = #(
#{
MachineName = "AAA"
ID = "111"
TagName = "GroupA"
},
#{
MachineName = "BBB"
ID = "222"
TagName = "GroupB"
}
}
You can use Where-Object(or alias name where). Where-Object returns all objects for which the script block statement is true
$dataset1 = $dataset1 | Where-Object { $_.TagName }

IF - Missing '=' operator after key in hash literal

Hello I am getting error "Missing '=' operator after key in hash literal." on the IF statement, where am I wrong
$Nests | ForEach-Object {
$RecordMetrics = [pscustomobject]#{
key = $_.key
count = $_.doc_count
if (key -like 'CBT99*')
{
bo = 'CBT'
SiteID = 1972
}
elseif
{
boperator = $map[$_.key.TrimEnd("1","2","3","4","5","6","7","8","9","0","-") ].boperator
SiteID = $map[$_.key.TrimEnd("1","2","3","4","5","6","7","8","9","0","-") ].SiteID
}
}}
You can't put a conditional statement in the middle of a hashtable literal like that.
You'll have to create the hashtable/dictionary first, then populate the relevant keys based on your conditional logic:
$Nests | ForEach-Object {
# create dictionary with initial properties
$properties = [ordered]#{
key = $_.key
count = $_.doc_count
Host = ''
OperatorName = ''
}
# conditionally add remaining properties
if ($_.key -like 'CBT99*') {
$properties['bo'] = 'CBT'
$properties['SiteID'] = 1972
}
else {
$properties['boperator'] = $map[$_.key.TrimEnd("1", "2", "3", "4", "5", "6", "7", "8", "9", "0", "-") ].boperator
$properties['SiteID'] = $map[$_.key.TrimEnd("1", "2", "3", "4", "5", "6", "7", "8", "9", "0", "-") ].SiteID
}
# convert to custom object
$RecordMetrics = [pscustomobject]$properties
}

groupby results of a matching function

given the below sample json, what i need to do, is to find if each value found in serialno, exists in serialnopool. If this holds TRUE, then i need a way, to have the values of serialno, that were found in serialnopool, grouped, along with their corresponding row ids.
example output of serialno value 11079851 that exists in serialnopool:
11079851: 098, 798, etc, each serialno value would go on with its new line.
Follows the sample json:
[
{
"rowid": "098",
"serialno": 11079851,
"serialnopool": 11079851
},
{
"rowid": 110,
"serialno": 11089385,
"serialnopool": 25853201
},
{
"rowid": 118,
"serialno": 11089385,
"serialnopool": 22412115
},
{
"rowid": 798,
"serialno": 11079851,
"serialnopool": 22412115
},
{
"rowid": "",
"serialno": "",
"serialnopool": 5423
},
{
"rowid": "",
"serialno": "",
"serialnopool": 5421312
}
]
How could this be achieved with the use of jq?
Assuming that by "exists in serialnopool" you mean "occurs at least once in the set of serialnopool values", the following is a solution provided that your jq is version 1.5 or higher:
map(.serialnopool) as $snp
| (INDEX( group_by(.serialno)[]; .[0].serialno|tostring)
| map_values(map(.rowid))) as $dict
| (map(.serialno) | unique[]) as $sn
| if IN($sn; $snp[])
then "\($sn): " + ($dict[$sn|tostring] | join(", "))
else empty end
The main point of interest in this solution is the dictionary ($dict), which maps .serialno|tostring values to the corresponding .rowid values.

Convert Json Object to string with PowerShell

I'm trying to convert this :
tags: [
{ tagName : "O365" },
{ tagName : "Skype for Business" }
]
to "O365,Skype for Business" as a string.
Could anyone help me to accomplish this using Powershell ?
Works only if you have only one "tags" property in your JSON :
$json = #"
{
tags: [
{ tagName : "O365" },
{ tagName : "Skype for Business" }
]
}
"#
(ConvertFrom-Json $json | % tags | % tagName) -Join ", "
# result : O365, Skype for Business
Similarly,
$separator = ","
$tagname = (ConvertFrom-Json $json).tags.tagName
[String]::Join($separator,$tagname)
Or as a one-liner
[String]::Join(",",(ConvertFrom-Json $json).tags.tagName)