Reputation: 9
I'm working with Valhalla to retrieve trips durations. In the documentation the format to send the request is as follows:
{"sources":[{"lat":40.744014,"lon":-73.990508}],"targets":[{"lat":40.744014,"lon":-73.990508},{"lat":40.739735,"lon":-73.979713},{"lat":40.752522,"lon":-73.985015},{"lat":40.750117,"lon":-73.983704},{"lat":40.750552,"lon":-73.993519}],"costing":"pedestrian"}&id=ManyToMany_NYC_work_dinner
I'm creating the source and the targets independently, this is the code for the source:
'{"sources":[{"lat": 19.690742994833027, "lon": -99.1680363639107]'
The targets come from a Pandas' series:
<style type="text/css">
.tg {
border-collapse: collapse;
border-spacing: 0;
}
.tg td {
border-color: black;
border-style: solid;
border-width: 1px;
font-family: Arial, sans-serif;
font-size: 14px;
overflow: hidden;
padding: 10px 5px;
word-break: normal;
}
.tg th {
border-color: black;
border-style: solid;
border-width: 1px;
font-family: Arial, sans-serif;
font-size: 14px;
font-weight: normal;
overflow: hidden;
padding: 10px 5px;
word-break: normal;
}
.tg .tg-0pky {
border-color: inherit;
text-align: left;
vertical-align: top
}
.tg .tg-0lax {
text-align: left;
vertical-align: top
}
</style>
<table class="tg" style="undefined;table-layout: fixed; width: 433px">
<colgroup>
<col style="width: 91px">
<col style="width: 342px">
</colgroup>
<thead>
<tr>
<th class="tg-0pky">Reference</th>
<th class="tg-0pky">order_point</th>
</tr>
</thead>
<tbody>
<tr>
<td class="tg-0pky">1</td>
<td class="tg-0pky">[19.690742994833027,-99.1680363639107]</td>
</tr>
<tr>
<td class="tg-0lax">2</td>
<td class="tg-0lax">[19.67109, -99.16845]</td>
</tr>
<tr>
<td class="tg-0lax">3</td>
<td class="tg-0lax">[19.684034594944944, -99.15103970155211]</td>
</tr>
<tr>
<td class="tg-0lax">4</td>
<td class="tg-0lax">[19.686485, -99.23003]</td>
</tr>
<tr>
<td class="tg-0lax">5</td>
<td class="tg-0lax">[19.718996, -99.13548]</td>
</tr>
</tbody>
</table>
trgt_dicts = [{"lat": i[0], "lon": i[1]} for i in targets]
Which returns:
[{'lat': 19.690742994833027, 'lon': -99.1680363639107}, {'lat': 19.67109, 'lon': -99.16845}, {'lat': 19.684034594944944, 'lon': -99.15103970155211}, {'lat': 19.686485, 'lon': -99.23003}, {'lat': 19.718996, 'lon': -99.13548}]
So, when I put everything together:
query = '{"sources":[{"lat": 'f'{source[0]}'', "lon": 'f'{source[1]}''], "targets": 'f'{trgt_dicts}'', "costing":"auto"}'
What I get is this:
'{"sources":[{"lat": 19.690742994833027, "lon": -99.1680363639107], "targets": [{\'lat\': 19.690742994833027, \'lon\': -99.1680363639107}, {\'lat\': 19.67109, \'lon\': -99.16845}, {\'lat\': 19.684034594944944, \'lon\': -99.15103970155211}, {\'lat\': 19.686485, \'lon\': -99.23003}, {\'lat\': 19.718996, \'lon\': -99.13548}], "costing":"auto"}'
Which retrieves and error, and I think the reason is the \'lon\': I'm getting instead of "lon":
I have tried with json.dumps in an attempt to get the right format as suggested in other questions. I also created variables for the source and the targets and put them together after playing around with the f-string:
qsource = '{"sources":[{"lat": 'f'{source[0]}'', "lon": 'f'{source[1]}''], "targets":'
qtrgt = ','.join(map(str, [trgt_dicts]))
query = qsource + qtrgt + ',"costing":"auto"}'
But at the end, no matter what, I end up with this \'lon\':
Upvotes: 0
Views: 55
Reputation: 312440
I think you mean for query
to be a dictionary, but you're creating a string (by appending a bunch of substrings). Take a closer look at your syntax:
query = '{"sources":[{"lat": 'f'{source[0]}'', "lon": 'f'{source[1]}''], "targets": 'f'{trgt_dicts}'', "costing":"auto"}'
If we split up the substrings to make it more clear, this is what you've written:
query = (
'{"sources":[{"lat": '
+ f"{source[0]}"
+ ', "lon": '
+ f"{source[1]}"
+ '], "targets": '
+ f"{trgt_dicts}"
+ ', "costing":"auto"}'
)
And honestly, that's just weird. I think what you want is something like:
query = {
"sources": [
{
"lat": source[0],
"lon": source[1],
},
],
"targets": trgt_dicts,
}
That gives you a Python dictionary. If you want to JSON-encode it, you
can use json.dumps
. Assuming that you have:
>>> source
[40.744014, -73.990508]
>>> trgt_dicts
[
{"lat": 40.744014, "lon": -73.990508},
{"lat": 40.739735, "lon": -73.979713},
{"lat": 40.752522, "lon": -73.985015},
{"lat": 40.750117, "lon": -73.983704},
{"lat": 40.750552, "lon": -73.993519},
]
This gives you:
>>> query = {
... "sources": [
... {
... "lat": source[0],
... "lon": source[1],
... },
... ],
... "targets": trgt_dicts,
... }
>>> print(json.dumps(query, indent=2))
{
"sources": [
{
"lat": 40.744014,
"lon": -73.990508
}
],
"targets": [
{
"lat": 40.744014,
"lon": -73.990508
},
{
"lat": 40.739735,
"lon": -73.979713
},
{
"lat": 40.752522,
"lon": -73.985015
},
{
"lat": 40.750117,
"lon": -73.983704
},
{
"lat": 40.750552,
"lon": -73.993519
}
]
}
Upvotes: 1