1

This question is similar to this question and has help from this question however while I think the json parse I am applying should work:

println(json.value[i].properties.instanceData)

result

{
   "Microsoft.Resources" : {
      "location" : "Asiseast",
      "tags" : {
         "costCentre" : "2222",
         "Department" : "DEPT",
         "Project" : "IaaS"
      },
      "resourceUri" : "/subscriptions/xxx-xxx-xxx-xxx/resourceGroups/RGNAME/providers/Microsoft.Compute/virtualMachines/VMNAME",
      "additionalInfo" : {
         "ServiceType" : "",
         "ImageType" : "",
         "VMProperties" : "",
         "UsageType" : "DataTrOut",
         "VMName" : ""
      }
   }
}

I try to pull the value of 'Microsoft-Resources' out of this with the following query -

println(json.value[i].properties.instanceData["Microsoft.Resources"])

but I get the following response:

Caught: groovy.lang.MissingPropertyException: No such property: Microsoft.Resources for class: java.lang.String

Trying -

println(json.value[i].properties.instanceData."Microsoft.Resources")

result:

Caught: groovy.lang.MissingPropertyException: No such property: Microsoft.Resources for class: java.lang.String

For reference, the output to

println(json.value[i].properties)

is

[subscriptionId:xxx-xxx-xxx-xxx-xxx,     
     usageStartTime:2018-01-01T00:00:00+00:00, 
     usageEndTime:2018-01-02T00:00:00+00:00, 
     meterName:Premium Storage - Snapshots (GB), 
     meterRegion:AU East, 
     meterCategory:Storage, 
     meterSubCategory:Locally Redundant, 
     unit:GB, 
     instanceData:
          {
           "Microsoft.Resources":{
               "resourceUri":"/subscriptions/ xxx-xxx-xxx-xxx-xxx /resourceGroups/RGNAME/providers/Microsoft.Storage/storageAccounts/STGNAME",
               "location":"aueast",
               "tags":{
                   "Project":"XXX",
                   "costCentre":"1234"
                 }
            }
       },
     meterId:b74c1bd6-c0ea-4248-b00a-dfe2afce7af0, 
     infoFields:[:], 
     quantity:0.005514

]

Mondo
  • 163
  • 13
  • Would you mind showing full json? or try `println json.value[i].properties.instanceData."Microsoft.Resources"`? – Rao Feb 15 '18 at 04:58
  • I've updated the post to show more detail. The full json is huge so I've just gone up to .properties – Mondo Feb 15 '18 at 05:15
  • Have you tried the one provided in above comment? What happened? – Rao Feb 15 '18 at 05:20
  • Yes, the result is shown in the edited original post (I just added formatting to make it clearer). – Mondo Feb 15 '18 at 05:28
  • Not sure from your comment that if resolved the issue that you are facing. Please clarify if you are still in need of solution with details. – Rao Feb 15 '18 at 05:52
  • Sorry, should have been clearer. No, the query you proposed has the same result - Caught: groovy.lang.MissingPropertyException: No such property: Microsoft.Resources for class: java.lang.String – Mondo Feb 15 '18 at 06:24

1 Answers1

2

This happends beacause json.value[i].properties.instanceData is instance of String as described in error message.

You need to parse this string to json.

def parsedJson = new groovy.json.JsonSlurper().parseText(json.value[i].properties.instanceData)
println parsedJson['Microsoft.Resources']
Evgeny Smirnov
  • 2,886
  • 1
  • 12
  • 22