Search code examples
jsonpowershellpowershell-4.0

Powershell access property stored in JSON object


$primaryEndpoints = az storage account show --resource-group rg --name sto --query 'primaryEndpoints'

The above command returns

{
  "blob": "https://sto.blob.core.windows.net/",
  "dfs": null,
  "file": "https://sto.file.core.windows.net/",
  "internetEndpoints": null,
  "microsoftEndpoints": null,
  "queue": "https://sto.queue.core.windows.net/",
  "table": "https://sto.table.core.windows.net/",
  "web": null
}

But his command below returns nothing :

echo $primaryEndpoints["blob"]

I've also tried

echo $primaryEndpoints.blob

How do I access the json property ?


Solution

  • It seems to me that you are getting a JSON string as a return value. To access the properties by name, you need to first convert the JSON string to a PSObject.

    $primaryEndpoints = az storage account show --resource-group rg --name sto --query 'primaryEndpoints'
    
    $primaryEndpointObjects = $primaryEndpoints | ConvertFrom-Json
    
    $primaryEndpointObjects.blob