Dinesh Madhup
Dinesh Madhup

Reputation: 367

Filter JSON ducument using PowerShell

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.

Current Result:

{
   "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
               }
            }
         }
      }
   ]
}

Expected Result:

[
      {
         "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
               }
            }
         }
      }
   ]

Code:

$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)

Revised Expected Output:

{
            "_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
               }
            }
         }

Original Code:

How to write data in Azure Blob storage programmatically?

Upvotes: 0

Views: 525

Answers (1)

Frank Borzage
Frank Borzage

Reputation: 6796

Please refer to my code:

$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)

Upvotes: 1

Related Questions