skip to Main Content

I need PreTaxCost and ResourceGroup out of this json, for further operations.

{

"id": "subscriptions/00000000-0000-0000-0000-000000000000/providers/Microsoft.CostManagement/Query/00000000-0000-0000-0000-000000000000",

"name": "55312978-ba1b-415c-9304-cfd9c43c0481",

"type": "microsoft.costmanagement/Query",

"properties": {

"nextLink": null,

"columns": [

  {

    "name": "PreTaxCost",

    "type": "Number"

  },

  {

    "name": "ResourceGroup",

    "type": "String"

  },

  {

    "name": "Currency",

    "type": "String"

  }

],

"rows": [

  [

    0.009865586851323632,

    "Ict_StratAndPlan_GoldSprova_Prod_0",

    "USD"

  ],

  [

    218.68795741935486,

    "Ict_StratAndPlan_GoldSprova_Prod_1",

    "USD"

  ],

  [

    2.10333307059661,

    "ScreenSharingTest-peer1",

    "USD"

  ],

  [

    0.14384913581657052,

    "Ssbciotelement01",

    "USD"

  ]

]

}

}

2

Answers


  1. Welcome to StackOverflow! Usually, the best way of parsing a json document, is to create an object structure that you can deserialize it to. I usually use a tool like https://json2csharp.com/. Make sure to check "Use Pascal Case" and "Use JsonPropertyName (.NET Core)". The generate class should look something like this:

    public class Column
    {
        [JsonPropertyName("name")]
        public string Name { get; set; }
    
        [JsonPropertyName("type")]
        public string Type { get; set; }
    }
    
    public class Properties
    {
        [JsonPropertyName("nextLink")]
        public object NextLink { get; set; }
    
        [JsonPropertyName("columns")]
        public List<Column> Columns { get; set; }
    
        [JsonPropertyName("rows")]
        public List<List<object>> Rows { get; set; }
    }
    
    public class Root
    {
        [JsonPropertyName("id")]
        public string Id { get; set; }
    
        [JsonPropertyName("name")]
        public string Name { get; set; }
    
        [JsonPropertyName("type")]
        public string Type { get; set; }
    
        [JsonPropertyName("properties")]
        public Properties Properties { get; set; }
    }
    

    From there you can deserialize the json using:

    var deserializedData = JsonSerializer.Deserialize<Root>(jsonString);
    

    Then getting the properties is easy with some C# code.

    Login or Signup to reply.
  2. You can use this set of class to give type to your json.

    public class NeededData
    {
       public double PreTaxCost{get; set;}
       public string ResourceGroup{get; set;}
    }
    
    public class Column
    {
        public string name { get; set; }
        public string type { get; set; }
    }
    
    public class Properties
    {
        public object nextLink { get; set; }
        public List<Column> columns { get; set; }
        public List<List<object>> rows { get; set; }
    }
    
    public class MainClass
    {
        public string id { get; set; }
        public string name { get; set; }
        public string type { get; set; }
        public Properties properties { get; set; }
    }
    

    After keeping these class in your code.

    var JsonString = ""; //Keep Your Json String Here.
    var filteredData = new List<NeededData>();
    var data = JsonSerializer.Deserialize<MainClass>(jsonString);
    foreach(var item in data.properties.rows){
        NeededData i = new NeededData();
        i.PreTaxCost = item[0];
        i.ResourceGroup = item[1];
        filteredData.Add(i);
    }
    

    The Code above will filter out the data you needed from the json string.I am Assuming the position of the data for column is accurate as it is in json string you provided.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search