0

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"]
    ]
  }
}
Simon
  • 1,244
  • 8
  • 21
  • 2
    Please edit your question so that your JSON is readable - at the moment half of it is unformatted, and there are blank lines all over the place. Additionally, it would really help if you'd show what you've tried already, as otherwise it looks like you're just treating Stack Overflow as a code-writing service. – Jon Skeet Jul 06 '23 at 07:49
  • Does this answer your question? [How can I deserialize JSON with C#?](https://stackoverflow.com/questions/6620165/how-can-i-deserialize-json-with-c) – phuzi Jul 06 '23 at 10:09

3 Answers3

0

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.

Simon
  • 1,244
  • 8
  • 21
0

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.

0

you can try this code

var jObj = JObject.Parse(json);

int PreTaxCostInd = ((JArray)jObj["properties"]["columns"]).ToList().FindIndex(x => (string)x["name"] == "PreTaxCost");
int ResourceGroupInd = ((JArray)jObj["properties"]["columns"]).ToList().FindIndex(x => (string)x["name"] == "ResourceGroup");

// or if you are sure you can replace code above with
int PreTaxCostInd = 0;
int ResourceGroupInd = 1;

List<CostGroup> result = ((JArray)jObj["properties"]["rows"])
                            .Select(x => new CostGroup
                            {
                                PreTaxCost = (double)x[PreTaxCostInd],
                                ResourceGroup = (string)x[ResourceGroupInd]
                            }).ToList();

public class CostGroup
{
    public double PreTaxCost { get; set; }
    public string ResourceGroup { get; set; }
}
Serge
  • 40,935
  • 4
  • 18
  • 45