使用PowerShell过滤JSON Ducument [英] Filter JSON ducument using PowerShell
本文介绍了使用PowerShell过滤JSON Ducument的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下代码,从端点读取JSON数据并将其存储在Azure Blob存储中.结果数据包含一些我不想存储在Blob存储中的额外订单项.
I have following code where I am reading JSON data from end point and storing in azure blob storage. The result data contains few extra line items which I don't want to store in Blob storage.
当前结果:
{
"total_rows":1,
"offset":0,
"rows":[
{
"id":"7f8a1b1779693cfebcfd3686f24a424a",
"key":"7f8a1b1779693cfebcfd3686f24a424a",
"value":{
"rev":"2-522bcd2edce6c1f1774f04cfaf3e86c6"
},
"doc":{
"_id":"7f8a1b1779693cfebcfd3686f24a424a",
"_rev":"2-522bcd2edce6c1f1774f04cfaf3e86c6",
"productId":"33218896",
"category":"Women's Clothing",
"manufacturer":"Contoso Sport",
"description":"Quick dry crew neck t-shirt",
"price":"14.99",
"shipping":{
"weight":1,
"dimensions":{
"width":6,
"height":8,
"depth":1
}
}
}
}
]
}
预期结果:
[
{
"id":"7f8a1b1779693cfebcfd3686f24a424a",
"key":"7f8a1b1779693cfebcfd3686f24a424a",
"value":{
"rev":"2-522bcd2edce6c1f1774f04cfaf3e86c6"
},
"doc":{
"_id":"7f8a1b1779693cfebcfd3686f24a424a",
"_rev":"2-522bcd2edce6c1f1774f04cfaf3e86c6",
"productId":"33218896",
"category":"Women's Clothing",
"manufacturer":"Contoso Sport",
"description":"Quick dry crew neck t-shirt",
"price":"14.99",
"shipping":{
"weight":1,
"dimensions":{
"width":6,
"height":8,
"depth":1
}
}
}
}
]
代码:
$Params = @{
"URI" = 'https://3ea5e53b-817e-4c41-ae0b-c5afc1610f4e-bluemix.cloudant.com/test/_all_docs?include_docs=true'
}
$Result = Invoke-RestMethod @Params | ConvertTo-Json -Depth 9
Write-Host "the result is :"
$Result
$context=New-AzStorageContext -StorageAccountName "andyprivate" -StorageAccountKey ""
$container=Get-AzStorageContainer -Name "input" -Context $context
$content = [system.Text.Encoding]::UTF8.GetBytes($Result)
$container.CloudBlobContainer.GetBlockBlobReference("my.json").UploadFromByteArray($content,0,$content.Length)
修订后的预期输出:
{
"_id":"7f8a1b1779693cfebcfd3686f24a424a",
"_rev":"2-522bcd2edce6c1f1774f04cfaf3e86c6",
"productId":"33218896",
"category":"Women's Clothing",
"manufacturer":"Contoso Sport",
"description":"Quick dry crew neck t-shirt",
"price":"14.99",
"shipping":{
"weight":1,
"dimensions":{
"width":6,
"height":8,
"depth":1
}
}
}
原始代码:
推荐答案
请参考我的代码:
$Params = @{
"URI" = 'https://3ea5e53b-817e-4c41-ae0b-c5afc1610f4e-bluemix.cloudant.com/test/_all_docs?include_docs=true'
}
$Result = Invoke-RestMethod @Params | ConvertTo-Json -Depth 9
$Jsonobject = ConvertFrom-Json $Result
$Rows = ConvertTo-Json $Jsonobject.rows
Write-Host "the result is :" $Result
$context=New-AzStorageContext -StorageAccountName "<your-storage-name>" -StorageAccountKey "<your-key>"
$container=Get-AzStorageContainer -Name "test" -Context $context
$content = [system.Text.Encoding]::UTF8.GetBytes($Rows)
$container.CloudBlobContainer.GetBlockBlobReference("my.json").UploadFromByteArray($content,0,$content.Length)
这篇关于使用PowerShell过滤JSON Ducument的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文