Search code examples
pythonjsongoogle-places-apigoogle-distancematrix-api

Display two different JSON results in one line


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.


Solution

  • 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.)