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

]

6
  • Would you mind showing full json? or try println json.value[i].properties.instanceData."Microsoft.Resources"? Commented Feb 15, 2018 at 4:58
  • I've updated the post to show more detail. The full json is huge so I've just gone up to .properties Commented Feb 15, 2018 at 5:15
  • Have you tried the one provided in above comment? What happened? Commented Feb 15, 2018 at 5:20
  • Yes, the result is shown in the edited original post (I just added formatting to make it clearer). Commented Feb 15, 2018 at 5: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. Commented Feb 15, 2018 at 5:52

1 Answer 1

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

Comments

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.