0

This is my json string returned from an ElasticSearch query:

var response = 
{
  "took":1,
  "timed_out":false,
  "_shards":
  {
    "total":5,
    "successful":5,
    "skipped":0,
    "failed":0
  },
  "hits":
  {
    "total":278,
    "max_score":0.0,
    "hits":[]
  },
  "aggregations":
  {
    "nombindiam": 
    {
      "doc_count_error_upper_bound":0,
      "sum_other_doc_count":0,
      "buckets":
      [
       {"key":15,"doc_count":12},
       {"key":20,"doc_count":24},
       {"key":25,"doc_count":44}
      ]
    }
  }
}

How to I get a list of integers with only the values of the key property of the buckets node like this:

List<int> myKeyValues= new List<int>() { 15, 20, 25 };

This is what I got so far:

JObject json = JObject.Parse(response);
var myKeyValues = json["aggregations"]["nombindiam"]["buckets"].ToList();
pdw
  • 5
  • 2

1 Answers1

0

You're nearly there, you just needed to Select the keys...

var myKeyValues = json["aggregations"]["nombindiam"]["buckets"]
    .Select(x => x["key"])
    .ToList();
Jono
  • 1,964
  • 4
  • 18
  • 35