Azure / Azure/azure-powershell
Invoke-AzCostManagementQuery - ConvertFrom-Json failed with error: Unexpected Character
- Dominant language
- C#
- Stars
- 4.8k
- Forks
- 4.3k
- Avg merge
- 3d 14h
- Merged PRs (30d)
- 54
Description
### Description
When attempting to run a usage query for a single storage account resource, I get an error for a convertfrom-Json taking place in the function
### Issue script & Debug output
```PowerShell
PS C:\Users\IHatch> Invoke-AzCostManagementQuery -Scope '/subscriptions/*****/resourceGroups/*****/providers/Microsoft.Storage/storageAccounts/*****' -TimeFrame LastMonth -Type Usage -DataSetGranularity 'Daily'
ConvertFrom-Json: C:\Program Files\WindowsPowerShell\Modules\Az.CostManagement\0.4.2\CostManagement.Autorest\custom\Invoke-AzCostManagementQuery.ps1:246:92
Line |
246 | … -Path $URL -Payload $Payload -Method POST).Content | ConvertFrom-Json
| ~~~~~~~~~~~~~~~~
| Conversion from JSON failed with error: Unexpected character encountered while parsing value: T. Path '', line 0, position 0.
```
### Environment data
```PowerShell
Name Value
---- -----
PSVersion 7.5.2
PSEdition Core
GitCommitId 7.5.2
OS Microsoft Windows 10.0.22631
Platform Win32NT
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1
WSManStackVersion 3.0
```
### Module versions
```PowerShell
ModuleType Version PreRelease Name ExportedCommands
---------- ------- ---------- ---- ----------------
Script 5.2.0 Az.Accounts {Add-AzEnvironment, Clear-AzConfig, Clear-AzContext, Clear-AzDefault…}
Script 0.4.2 Az.CostManagement {Get-AzCostManagementExport, Get-AzCostManagementExportExecutionHistory, Invoke-AzCostManagementExecuteExport, Invoke-AzCostManagementQuery…}
```
### Error output
```PowerShell
HistoryId: 4
Message : Conversion from JSON failed with error: Unexpected character encountered while parsing value: T. Path '', line 0, position 0.
StackTrace : at Microsoft.PowerShell.Commands.JsonObject.ConvertFromJson(String input, Boolean returnHashtable, Nullable`1 maxDepth, JsonDateKind jsonDateKind, ErrorRecord& error)
at Microsoft.PowerShell.Commands.ConvertFromJsonCommand.ConvertFromJsonHelper(String input)
at Microsoft.PowerShell.Commands.ConvertFromJsonCommand.EndProcessing()
at System.Management.Automation.CommandProcessorBase.Complete()
Exception : System.ArgumentException
InvocationInfo : {ConvertFrom-Json}
Line : $ResponseContent = (Invoke-AzRest -Path $URL -Payload $Payload -Method POST).Content | ConvertFrom-Json
Position : At C:\Program Files\WindowsPowerShell\Modules\Az.CostManagement\0.4.2\CostManagement.Autorest\custom\Invoke-AzCostManagementQuery.ps1:246 char:92
+ … -Path $URL -Payload $Payload -Method POST).Content | ConvertFrom-Json
+ ~~~~~~~~~~~~~~~~
HistoryId : 4
Message : Unexpected character encountered while parsing value: T. Path '', line 0, position 0.
StackTrace : at Newtonsoft.Json.JsonTextReader.ParseValue()
at Newtonsoft.Json.JsonReader.ReadForType(JsonContract contract, Boolean hasConverter)
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.Deserialize(JsonReader reader, Type objectType, Boolean checkAdditionalContent)
at Newtonsoft.Json.JsonSerializer.DeserializeInternal(JsonReader reader, Type objectType)
at Newtonsoft.Json.JsonConvert.DeserializeObject(String value, Type type, JsonSerializerSettings settings)
at Microsoft.PowerShell.Commands.JsonObject.ConvertFromJson(String input, Boolean returnHashtable, Nullable`1 maxDepth, JsonDateKind jsonDateKind, ErrorRecord& error)
Exception : Newtonsoft.Json.JsonReaderException
InvocationInfo : {ConvertFrom-Json}
Line : $ResponseContent = (Invoke-AzRest -Path $URL -Payload $Payload -Method POST).Content | ConvertFrom-Json
Position : At C:\Program Files\WindowsPowerShell\Modules\Az.CostManagement\0.4.2\CostManagement.Autorest\custom\Invoke-AzCostManagementQuery.ps1:246 char:92
+ … -Path $URL -Payload $Payload -Method POST).Content | ConvertFrom-Json
+ ~~~~~~~~~~~~~~~~
HistoryId : 4
```
Contributor guide
Research direction
Reproduce the single-resource query from the issue, then inspect CostManagement.Autorest/custom/Invoke-AzCostManagementQuery.ps1 at line 246 and the content returned by Invoke-AzRest before ConvertFrom-Json. Done means the command no longer produces the reported JSON parsing error and response handling is verified for this query.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- azure, powershell
- Domain
- api, cloud
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 35/100