0

I am requesting weather data via API requests for a given coordinate. One request allows me to get 10 days of data. I would like to make multiple requests in order to get historical data. I am unsure how to merge the requests into one JSON file.

At the moment, I'm creating a list of JSON requests. How do merge into one JSON?

import requests
import pyarrow as arrow


response_list=[]

for dates in dates:
  start = arrow.get(dates)

  response = requests.get(
    'https://-URL',
    params={
      foo:bar
      'start': start
      'end': start.shift(days=10)

    },
    headers={
      'Authorization': api_key
    }
  )

response_list.append(response.json())

# Do something with response data.

I get all the data in a list, but I want it all in 1 JSON object instead of a list of JSON objects.

Maybe this will help:

print("response 1 dates: ")
print(response_list[0]["meta"])

print("response all dates: ")
print(response_list["meta"])

Output:

response 1 dates: 
{'cost': 1, 'dailyQuota': 50, 'end': '2019-10-26 00:00', 'lat': -8.848808, 'lng': 115.171059, 'params': ['swellDirection'], 'requestCount': 22, 'start': '2019-10-25 00:00'}
response all dates: 
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-83-6f4ff2d1a0b8> in <module>()
      5 
      6 print("response all dates: ")
----> 7 print(response_list["meta"])
      8 
      9 

TypeError: list indices must be integers or slices, not str
Martin Gergov
  • 1,556
  • 4
  • 20
  • 29
  • It would help to see sample data so we can understand how it might be merged. And also it would help to see the final format you want to create – ADyson Nov 09 '19 at 09:10
  • I have added sample code. As you can see, I have a list of JSON - I want to merge the list to get 1 single JSON file, as opposed to a list of JSON – Dan0z_Direct Nov 09 '19 at 10:22
  • Did you miss two indents before `response_list.append(response.json())`? – LiuChang Nov 09 '19 at 11:32
  • Please [provide a reproducible copy of the DataFrame with `to_clipboard`](https://stackoverflow.com/questions/52413246/provide-a-reproducible-copy-of-the-dataframe-with-to-clipboard/52413247#52413247) – Trenton McKinney Nov 11 '19 at 07:27

0 Answers0