[英]Powershell: High Performing conversion of Name and Value Array into Parseable format - How can I make this Faster
How can I make my code a lot more performant when I wish to make an easily/fast parseable object/PSCustomObject from a JSON payload $JSON
?当我希望从 JSON 有效负载
$JSON
制作一个容易/快速的可解析对象/PSCustomObject 时,如何使我的代码性能更高?
An example of the structure of the PAYLOAD I receive is:我收到的 PAYLOAD 结构的一个示例是:
[
{
"name": "system.enablenetflow",
"value": "false"
},
{
"name": "system.deviceGroupId",
"value": "186,3060"
},
{
"name": "system.prefcollectorid",
"value": "144"
},
{
"name": "system.collectorplatform",
"value": "windows"
}
]
As you can see its in a very annoying format.如您所见,它的格式非常烦人。
Note that the payloads I attempt to parse are much larger and variable in count from 500 of these Name/Value objects to 50000, rather than just the 4 listed above.请注意,我尝试解析的有效负载要大得多,并且数量从 500 个这些名称/值对象到 50000 个不等,而不仅仅是上面列出的 4 个。
########################################################################### ################################################# #########################
MY GOAL我的目标
To have this turn into a key:value pair scenario for easier parsing later把它变成一个键值对场景,以便以后更容易解析
NOT This :不是这个:
With the JSON I have to do $JSON.where({$_.name -eq "system.enablenetflow"}).value
使用 JSON 我必须做
$JSON.where({$_.name -eq "system.enablenetflow"}).value
YES THIS :是的:
I want the end state to be that the new variable $obj
I create will let me get the value with $obj."system.enablenetflow"
我希望结尾 state 是我创建的新变量
$obj
将让我获得$obj."system.enablenetflow"
########################################################################### ################################################# #########################
MY CURRENT ATTEMPT THAT IS SUPER SLOW我目前的尝试非常慢
I did the following:我做了以下事情:
$obj
$obj
$JSON
variable which iterated through the JSON Array$JSON
变量执行了 foreach 方法$obj
with setting the 'name' as PropertyName and 'value' as PropertyValue$obj
Heres a sample of my code:这是我的代码示例:
$obj = [PSCustomObject] @{}
$json.foreach({
$thisitem = $_
$obj | Add-member -NotePropertyName $($thisitem.name) -NotePropertyValue $($thisitem.name)
})
HOW CAN I MAKE THIS FASTER?我怎样才能让它更快?
# Sample input JSON.
$json = @'
[
{
"name": "system.enablenetflow",
"value": "false"
},
{
"name": "system.deviceGroupId",
"value": "186,3060"
},
{
"name": "system.prefcollectorid",
"value": "144"
},
{
"name": "system.collectorplatform",
"value": "windows"
}
]
'@
# Initialize the (ordered) result hashtable.
$result = [ordered] @{}
# Note: In PowerShell (Core) 7+, add -AsHashTable to the ConvertFrom-Json
# call for additional performance gain, combined with -AsArray,
# in which case you don't need the `(...)` around the call anymore.
foreach ($element in (ConvertFrom-Json $json)) {
$result[$element.name] = $element.value
}
The above creates an (ordered) hashtable instead of a [pscustomobject]
instance - especially if the latter are iteratively constructed via Add-Member
calls.上面创建了一个(有序的)哈希表而不是一个
[pscustomobject]
实例 - 特别是如果后者是通过Add-Member
调用迭代构造的。
Hashtables are lighter-weight and faster to construct than [pscustomobject]
instances.哈希表比
[pscustomobject]
实例更轻量且构建速度更快。
Using a foreach
loop rather than processing the ConvertFrom-Json
output in a pipeline via ForEach-Object
also speeds up processing.使用
foreach
循环而不是通过ForEach-Object
在管道中处理ConvertFrom-Json
output 也可以加快处理速度。
PowerShell allows you to use the familiar dot notation also with hashtables; PowerShell 允许您在哈希表中也使用熟悉的点表示法; so, for instance, after running the above, you'll get:
因此,例如,在运行上述内容后,您将获得:
PS> $result.'system.collectorplatform'
windows
If you do need $result
to be a [pscustomobject]
instance, you can simply cast the fully populated hashtable to that type:如果您确实需要
$result
作为[pscustomobject]
实例,您可以简单地将完全填充的哈希表转换为该类型:
PS> $obj = [pscustomobject] $result; $obj.'system.collectorplatform'
windows
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.