使用 PowerShell 过滤 JSON 文档
Filter JSON ducument using PowerShell
我有以下代码,我从端点读取 JSON 数据并存储在 azure blob 存储中。结果数据包含一些我不想存储在 Blob 存储中的额外行项目。
当前结果:
{
"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)
我有以下代码,我从端点读取 JSON 数据并存储在 azure blob 存储中。结果数据包含一些我不想存储在 Blob 存储中的额外行项目。
当前结果:
{
"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)