0

I get a QueryResponse.Data returning the following data in table format with two columns "type" and name":

Content: {"columns": [{"name": "type","type": "string"},{"name": "name","type": "string"}],"rows": [["microsoft.apimanagement/service","api-management-name1"],...]

The final query will have much more fields and might be dynamically adjusted? What is a scalable way to convert to a csv file?

// Table or ObjectArray
    request.Options = new QueryRequestOptions() { Top = 50, ResultFormat = ResultFormat.Table };
    QueryResponse response = argClient.Resources(request);
    log.LogInformation("Count: " + response.Count);
    List<object> results = new List<object>();
    if (response.Count > 0)
    {
        // Continue till SkipToken is null
        while (!string.IsNullOrWhiteSpace(response.SkipToken))
        {
            // Update request with new skip token returned from response
            request.Options.SkipToken = response.SkipToken;
            // Send query with SkipToken to the ResourceGraphClient and get response
            response = argClient.Resources(request);
            log.LogInformation("Skip: " + response.SkipToken);
            //todo narrow down rows
            results.Add(response.Data);
        }
    }
    log.LogInformation("Content count : " + results.Count);
    foreach (var row in results)
    {
        log.LogInformation("Content: " + row.ToString());
    }

Thanks!

Edit: I seem to be close with log.LogInformation(res["rows"]) resulting in the following:

[["microsoft.apimanagement/service","name1"],["microsoft.apimanagement/service","name2"],["microsoft.apimanagement/service","name3"],["microsoft.apimanagement/service","name4"]
...]

However res["rows"].toString() returns 'JToken' does not contain a definition for 'toString' and no accessible extension method 'toString' accepting a first argument of type 'JToken' could be found (are you missing a using directive or an assembly reference?) Any possibility to get straight to csv? If not, what is the recommended way to automate Azure Resource Graph csv creation? Writing a console application?

wuz
  • 483
  • 3
  • 16

1 Answers1

0

Given from your example data I would create some corresponding classes like:

public class Column
{
    public string Name { get; set; }
    public string Type { get; set; }
}

public class Table
{
    public List<Column> Columns { get; set; }
    public List<string[]> Rows { get; set; }
}

With this you could deserialize the stuff more or less by this call:

var table = JsonConvert.Deserialize<Table>(response.Data);

Writing these information as a CSV file would be something like this:

file.WriteLine(string.Join(",", table.Columns));
foreach (var row in table.Rows)
{
    file.WriteLine(string.Join(",", row));
}

That should give you a first sketch on how to solve problem. Maybe you like to quote all values or something similar, but that's another question.

Oliver
  • 43,366
  • 8
  • 94
  • 151
  • Hi, thanks for the answer. The query will be quite dynamic with different fields. Therefore I would try to avoid "hardcoding the fields in a class" and just print the structure as csv. – wuz Mar 10 '22 at 12:29