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 ?

4
  • How are you running it? Commented Apr 6, 2021 at 6:04
  • using VSCode.And have mentioned the code snippet in the question. Commented Apr 6, 2021 at 6: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 Commented Apr 6, 2021 at 7: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... Commented Apr 6, 2021 at 8:07

1 Answer 1

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 
Sign up to request clarification or add additional context in comments.

6 Comments

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 ?
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 Please try to use $JSON_obj|ConvertTo-JSON|Out-File.
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 please refer to stackoverflow.com/questions/32959431/…?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.