0

I am new to JSON, Google Maps APIs DistanceMatrix & Places
I am trying to display results from two different JSON Results

Basically both of them come from two different "lists"

edit
for object_json i am getting all the nearby places from my coordinates, I will then get the places_id of each result and use it to query the distance-matrix api and get each of distances object_json2

Code

    #name and types from Places API
    lines = (f"{s['name']}: {', '.join(r.replace('_', ' ') for r in s['types'])} -" for s in object_json['results'][0:5])
    #distance from DistanceMatrix API
    lines2 = (f"{t['distance']['text']}" for t in item['elements'] for item in object_json2['rows']) 

Sample JSON object_json: getting the name, types and places_id

   "results" : [
      {
         "name" : "Joe’s Thai Kitchen",
         "place_id" : "ChIJIadbw8sb2jER3i-OvZSCBGk",
         "types" : [ "restaurant", "food", "point_of_interest", "establishment" ],
      },

object_json2 based on the nearby search results, it will get the places_id and get the distance matrix results for each elements distance.

"rows" : [
      {
         "elements" : [
            {
               "distance" : {
                  "text" : "0.3 km",
                  "value" : 303
               },
               "duration" : {
                  "text" : "2 mins",
                  "value" : 123
               },
               "status" : "OK"
            },
            {
               "distance" : {
                  "text" : "90 m",
                  "value" : 90
               },
               "duration" : {
                  "text" : "1 min",
                  "value" : 36
               },
               "status" : "OK"
            }
         ]
      }
   ],

First two results

Joe’s Thai Kitchen: restaurant, food, point of interest, establishment - ['0.4 km', '77 m', '0.3 km', '0.8 km', '0.3 km']

Hong Kong Street Chun Kee: restaurant, food, point of interest, establishment - ['0.4 km', '77 m', '0.3 km', '0.8 km', '0.3 km']

Desired first two results

Joe’s Thai Kitchen: restaurant, food, point of interest, establishment - 0.4 km
Hong Kong Street Chun Kee: restaurant, food, point of interest, establishment - 77 m

Hope somebody can help thank you.

blueKingdom11
  • 103
  • 10
  • 1
    Add a sample of `object_json2` and `object_json`. They would look like [Distance Matrix Responses](https://developers.google.com/maps/documentation/distance-matrix/intro#DistanceMatrixResponses) but we don't know what your initial requests/queries were. Under `rows`, what is your criteria for which `elements` you want to use for the distances? (Since you're not using the remainder.) Is it just the first distance element of the first result? – aneroid Mar 12 '20 at 15:22
  • 2
    Please provide a [mcve]. That first code snippet really needs some refactoring. – AMC Mar 12 '20 at 19:50
  • @aneroid thanks for your reply. I added the same of the `object_json` and `object_json2` results. Basically I want to get every distance under the elements because it is all the nearby search results. – blueKingdom11 Mar 18 '20 at 12:43

1 Answers1

0

Firstly, I don't think you're interpreting the results from the Distance Maxtrix API correctly. This is also leading to the XY Problem.

But in any case, what you're actually looking to do is join the results of two lists in object1 and object2. You can do this with zip(). zip() takes each iterable given and returns a tuple of each lists' nth element. And stops at the shortest one:

>>> list(zip('ABCD', 'xyz'))
[('A', 'x'), ('B', 'y'), ('C', 'z')]

Assuming object_json looks like this:

object_json = {
    "results" : [
        {
            "name" : "Joe’s Thai Kitchen",
            "place_id" : "ChIJIadbw8sb2jER3i-OvZSCBGk",
            "types" : [ "restaurant", "food", "point_of_interest", "establishment" ],
        },
        {
            "name" : "Hong Kong Street Chun Kee",
            "place_id" : "KgbcszVo-I3reJ2BS8WBDAijiHc",
            "types" : [ "restaurant", "food", "point_of_interest", "establishment" ],
        }
    ]
}

Using object_json2 as per your example:

>>> lines = (f"{s['name']}: {', '.join(r.replace('_', ' ') for r in s['types'])} -" for s in object_json['results'][0:5])
>>> lines2 = (f"{t['distance']['text']}" for item in object_json2['rows'] for t in item['elements'])
>>>
>>> [' '.join((x, y)) for x, y in zip(lines, lines2)]
['Joe’s Thai Kitchen: restaurant, food, point of interest, establishment - 0.3 km',
 'Hong Kong Street Chun Kee: restaurant, food, point of interest, establishment - 90m'
]

(Btw, in your code this line is a syntax error: lines2 = (f"{t['distance']['text']}" for t in item['elements'] for item in object_json2['rows']). The 2nd for loop should come first.)

aneroid
  • 12,983
  • 3
  • 36
  • 66
  • Thank you for your help that did the trick. I added `lines3 = *4th line* str1 = '\n\n'.join(lines3)` to convert it to string and display it accordingly. Apologies for the error on the code but thank you for correcting me! Again thanks for your help – blueKingdom11 Mar 21 '20 at 15:37