0

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"
}




1
  • Have you tried anything yet? Maybe you already looped over the file entries to get the values? Commented Oct 9, 2020 at 13:20

1 Answer 1

1
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.
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks alot!! My double looks like this: var result = new double[numStops, numStops]; How can I convert it to an two dimensional array.?
You want an two dimensional array with same values?
Yeh similiar to a distance matrix. for (int j = 0; j < (numStops*numStops);){ for (int i = 0; i < numStops; i++){ for (int h = 0; h < numStops; h++){ result[i,h] = arr[j]; j++; } } }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.