1

I am trying to alter the default response of Azure Resource graph query into similar that Azure portal uses. My query is:

resourcecontainers | where type == "microsoft.resources/subscriptions" | project name, tags

From where the response for tags is:

  "tags": {
    "TagA": "TeamA",
    "TagB": "TeamB",
    "TagC": "TeamC"
  },

I would like to alter this into:

    "tags": [
  {
    "name": "TagA",
    "value": "TeamA"
  },
  {
    "name": "TagB",
    "value": "TeamB"
  },
  {
    "name": "TagC",
    "value": "TeamC"
  }
]

How to do it? All examples I have found are either for only one tag or static set of tags. Mine would need to support dynamic amount of tags.

Ansuman Bal
  • 9,705
  • 2
  • 10
  • 27
Kamsiinov
  • 1,315
  • 2
  • 20
  • 50
  • 1
    Could you please try with this `cmd` `resourcecontainers | where subscriptionId == "xxxxxxxxxxxxxxxxxxxxxxxxxxxx" | where type =~ 'microsoft.resources/subscriptions'| mvexpand parsejson(tags)| extend tagname = tostring(bag_keys(tags)[0])| extend tagvalue = tostring(tags[tagname])| project name, tagname, tagvalue` This cmd will list with tag name and value . Is this the same you are looking for. – AjayKumarGhose Jan 16 '22 at 07:36
  • it is almost what I am looking for. In your solution the subscription name is added into each array item. I can remove it from the projection though and join it to my other query. – Kamsiinov Jan 17 '22 at 07:36
  • 1
    Okay i am posting the same as an answer by removing the subscription id – AjayKumarGhose Jan 17 '22 at 11:43

1 Answers1

1

As you have confirmed to achieve the above requirement we can do it using the below query

resourcecontainers      
| where type =~ 'microsoft.resources/subscriptions'
| mvexpand parsejson(tags)
| extend tagname = tostring(bag_keys(tags)[0])
| extend tagvalue = tostring(tags[tagname])
| project name, tagname, tagvalue

Output:- enter image description here

For more information please refer this BLOG

AjayKumarGhose
  • 4,257
  • 2
  • 4
  • 15