0

I have a JSON , like so(posting just the initial snippet) :

[
  {
    "ICloudBlob":      {
      "StreamWriteSizeInBytes":  4194304,
      "ServiceClient":  {
        "AuthenticationScheme":  1,
        "BufferManager":  null,
        "Credentials":      {
          "SASToken":  null,
          "AccountName":
          "storageaccountappse9a4d",
          "KeyName":  null,
          "IsAnonymous":  false,
          "IsSAS":  false,
          "IsSharedKey":  true,
          "IsToken":  false,
          "SASSignature":  null
        },
        "BaseUri":
        "https://storageaccountappse9a4d.blob.core.windows.net/",
        "StorageUri":  {
          "PrimaryUri":
          "https://storageaccountappse9a4d.blob.core.windows.net/",
          "SecondaryUri":  null
        },
        "DefaultRequestOptions":  {
          "RetryPolicy":  {

          },
          "EncryptionPolicy":  null,
          "RequireEncryption":  null,
          "CustomerProvidedKey":  null,
          "EncryptionScope":  null,
          "AbsorbConditionalErrorsOnRetry":  null,
          "LocationMode":  0,
          "ServerTimeout":  null,
          "MaximumExecutionTime":  null,
          "NetworkTimeout":  null,
          "ParallelOperationThreadCount":  1,
          "SingleBlobUploadThresholdInBytes":  134217728,
          "UseTransactionalMD5":  null,
          "StoreBlobContentMD5":  null,
          "DisableContentMD5Validation":  null,
          "ChecksumOptions":  {
            "DisableContentMD5Validation":  null,
            "StoreContentMD5":  null,
            "UseTransactionalMD5":  null,
            "DisableContentCRC64Validation":  null,
            "UseTransactionalCRC64":  null
          }

I want to access the ParallelOperationThreadCount parameter, for which i'm using the following code:

$JSON_obj=Get-AzStorageAccount | Get-AzStorageContainer | Get-AzStorageBlob | ConvertTo-JSON -Depth 
50
$ParallelOperationThreadCount=@()
foreach($i in $JSON_obj)
{
$ParallelOperationThreadCount+=$i.ICloudBlob.ServiceClient.DefaultRequestOptions.ParallelOperationThreadCount

 }
$ParallelOperationThreadCount

However,on running this,no output comes out. It runs and then exits. Any way to figure out what might work ?

Lieven Keersmaekers
  • 53,391
  • 11
  • 100
  • 140
Rakshita
  • 67
  • 6
  • How are you running it? – Lieven Keersmaekers Apr 06 '21 at 06:04
  • using VSCode.And have mentioned the code snippet in the question. – Rakshita Apr 06 '21 at 06:08
  • 1
    If you do `ConvertTo-JSON`, you converting your `object` to a (JSON) `string`. If you want to convert a (JSON) `string` to an `object`, you should use: [`ConvertFrom-Json`](https://docs.microsoft.com/powershell/module/microsoft.powershell.utility/convertfrom-json) – iRon Apr 06 '21 at 07:34
  • 1
    I also wouldn't call your variable `$JSON_obj`, as that is confusing, it is just an object and has no longer a relation with Json (after you do a `ConvertFrom-Json`). Meaning, you can't iterate through a Json string, you're iterating through an object... – iRon Apr 06 '21 at 08:07

1 Answers1

2

The cmdlet ConvertTo-JSON will convert your results to json string. It will not return the result as an array. enter image description here

So I suggest you remove the cmdlet ConvertTo-JSON in the first line command. After doing that, the command return the result as object array. Then your script will return the right results. enter image description here

For example (I do that in one storage account)

Connect-AzAccount

$accountName =""
$groupName=""

$JSON_obj=(Get-AzStorageAccount -ResourceGroupName $groupName -Name $accountName  | Get-AzStorageContainer  | Get-AzStorageBlob )

$ParallelOperationThreadCount=@()
foreach($i in $JSON_obj)
{
$ParallelOperationThreadCount+=$i.ICloudBlob.ServiceClient.DefaultRequestOptions.ParallelOperationThreadCount

 }
$ParallelOperationThreadCount

enter image description here

Besides, we also can simplify the script as below.

$ParallelOperationThreadCount= (Get-AzStorageAccount -ResourceGroupName $groupName -Name $accountName  |
        Get-AzStorageContainer  | Get-AzStorageBlob|
        Select-Object -Property @{Name="ParallelOperationThreadCount";Expression={$_.ICloudBlob.ServiceClient.DefaultRequestOptions.ParallelOperationThreadCount}})

$ParallelOperationThreadCount

enter image description here


Update

Please refer to the following script

Connect-AzAccount

$accountName =""
$groupName=""

$JSON_obj=(Get-AzStorageAccount -ResourceGroupName $groupName -Name $accountName  | Get-AzStorageContainer  | Get-AzStorageBlob )

$ParallelOperationThreadCount=@()
foreach($i in $JSON_obj)
{
$ParallelOperationThreadCount+=$i.ICloudBlob.ServiceClient.DefaultRequestOptions.ParallelOperationThreadCount

 }
$ParallelOperationThreadCount

$JSON_obj|ConvertTo-JSON -Depth 50 | % { [System.Text.RegularExpressions.Regex]::Unescape($_) } | Out-File -Encoding Ascii - append C:\Users\rakshitas\Documents\json_excel\blob.json 
Jim Xu
  • 15,664
  • 2
  • 7
  • 21
  • so this works,thanks. I wanna store the json in a file as well,and it needs some changes hence using this command: $JSON=Get-AzStorageAccount | Get-AzStorageContainer | Get-AzStorageBlob | ConvertTo-JSON -Depth 50 | % { [System.Text.RegularExpressions.Regex]::Unescape($_) } | Out-File -Encoding Ascii - append C:\Users\rakshitas\Documents\json_excel\blob.json $JSON_obj = ConvertFrom-Json -InputObject $JSON. It doesn't work then,any suggestions ? – Rakshita Apr 06 '21 at 10:32
  • @Rakshita please check my update. – Jim Xu Apr 06 '21 at 12:45
  • This helps. There's another key value pair I wanted to access , it has a \u0027 unicode.The script I mentioned returns the json that way. But , doesn't return any output when I access feilds like now. Think it may be because of this code here : -Depth 50 | % { [System.Text.RegularExpressions.Regex]::Unescape($_) } ? – Rakshita Apr 06 '21 at 13:10
  • @Rakshita Please try to use `$JSON_obj|ConvertTo-JSON|Out-File`. – Jim Xu Apr 06 '21 at 13:14
  • Yeah, that is what I get in the outfile. Few entries with unicode with them. The regex code , removes them. But then I ain't able to access key-value pairs. You think it may be because of these specific line of code? – Rakshita Apr 06 '21 at 13:24
  • @Rakshita please refer to https://stackoverflow.com/questions/32959431/json-file-to-powershell-and-back-to-json-file? – Jim Xu Apr 06 '21 at 13:27
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/230805/discussion-between-rakshita-and-jim-xu). – Rakshita Apr 06 '21 at 13:44