PabloS
PabloS

Reputation: 81

Getting specific key values | JSON | C#

How do I manage to filter after specific key values in a json file? I am using C#. I want to get every distance.value and save them into an array of doubles. That way I want to create a distance matrix.

Stackoverflow wants me to add more information but I am not sure what additional code snippets are useful. If I am missing something I will add it to the comments.

Here is the json output.

{
   "destination_addresses" : [],
   "origin_addresses" : [],
   "rows" : [
      {
         "elements" : [
            {
               "distance" : {
                  "text" : "1 m",
                  "value" : 0
               },
               "duration" : {
                  "text" : "1 Minute",
                  "value" : 0
               },
               "status" : "OK"
            },
            {
               "distance" : {
                  "text" : "5 m",
                  "value" : 5
               },
               "duration" : {
                  "text" : "1 Minute",
                  "value" : 1
               },
               "status" : "OK"
            },
            {
               "distance" : {
                  "text" : "3,7 km",
                  "value" : 3730
               },
               "duration" : {
                  "text" : "10 Minuten",
                  "value" : 582
               },
               "status" : "OK"
            }
         ]
      },
      {
         "elements" : [
            {
               "distance" : {
                  "text" : "0,5 km",
                  "value" : 510
               },
               "duration" : {
                  "text" : "3 Minuten",
                  "value" : 182
               },
               "status" : "OK"
            },
            {
               "distance" : {
                  "text" : "1 m",
                  "value" : 0
               },
               "duration" : {
                  "text" : "1 Minute",
                  "value" : 0
               },
               "status" : "OK"
            },
            {
               "distance" : {
                  "text" : "3,7 km",
                  "value" : 3725
               },
               "duration" : {
                  "text" : "10 Minuten",
                  "value" : 581
               },
               "status" : "OK"
            }
         ]
      }    
   ],
   "status" : "OK"
}




Upvotes: 0

Views: 1293

Answers (1)

Sergey Nazarov
Sergey Nazarov

Reputation: 706

var json = "_YOUR_JSON_";
var jobject = JObject.Parse(json);
var doubles = jobject.SelectTokens("$.rows[*].elements[*].distance.value")
    .Values<double>()
    .ToArray();

Notes:

  • The query string rows[*] contains the JSONPath wildcard operator [*]. This operator matches all array elements under the parent element "rows".
  • Same as previous for elements[*]
  • distance.value - path to required value
  • As long as SelectTokens returns IEnumerable<JToken> we need to cast our values to double, so call Values<double>()
  • Finally cast our IEnumerable<double> to array.

Upvotes: 1

Related Questions