Search code examples
azurepowershellazure-storage-account

Read JSON from Azure Blob Storage using PowerShell


I have a following JSON file/blob in the Azure Storage as students.json. Using Powershell, I want to read the contents of the file, student's FirstName and LastName where the MainSubject is Math

{
  "college": {
    "students": [
      {
        "FirstName": "abc",
        "LastName": "xyz",
        "MainSubject": "Computers"
      },
      {
        "FirstName": "lmn",
        "LastName": "opq"
        "MainSubject": "Math"        
      }
    ]
  }
}


Solution

  • I have reproduced in my environment and got expected results as below and followed Microsoft-Document and Thanks to @ Ivan Yang's SO-thread :

        $accountName = "rithwikstor"
        $accountKey = "XX"
        $containerName = "rithwik"
        $blobName = "emo.json"
        $context = New-AzStorageContext -StorageAccountName $accountName -StorageAccountKey $accountKey
        $container_client = Get-AzStorageContainer -Name $containerName -Context $context
        $source_blob_client = $container_client.CloudBlobContainer.GetBlockBlobReference($blobName)
        $download_file = $source_blob_client.DownloadText()
        $jsonContent = $download_file | ConvertFrom-Json
        
        
        $ans=$jsonContent.college | ConvertTo-json
        $res= $ans | ConvertFrom-json
        $res.students | where{$_.Mainsubject -eq 'Math'}
        $emo=$res.students | where{$_.Mainsubject -eq 'Math'}
        $emo | Select-Object * -ExcludeProperty MainSubject
    
    
    

    XX is the Storage account key. Got it from below:

    enter image description here

    Output:

    enter image description here