Python Forum
How to save json data in a dataframe - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: How to save json data in a dataframe (/thread-33315.html)



How to save json data in a dataframe - shantanu97 - Apr-15-2021

When I am using the api, the data is coming in json format and I want to save all this data in dataframe.
import json
import requests

hostname = "http://geomag.bgs.ac.uk/web_service/GMModels/igrf/13/?latitude=-35&longitude=149&altitude=0&date=2021-03-23&format=json"

try: 
  response = requests.get(hostname)
  # extract JSON payload of response as Python dictionary
  json_payload = response.json()
  # raise an Exception if we encoutnered any HTTP error codes like 404 
  response.raise_for_status()
except requests.exceptions.ConnectionError as e: 
  # handle any typo errors in url or endpoint, or just patchy internet connection
  print(e)
except requests.exceptions.HTTPError as e:  
  # handle HTTP error codes in the response
  print(e, json_payload['error'])
except requests.exceptions.RequestException as e:  
  # general error handling
  print(e, json_payload['error'])
else:
  json_payload = response.json()
  print(json.dumps(json_payload, indent=4, sort_keys=True))



RE: How to save json data in a dataframe - klllmmm - Apr-15-2021

data = json.loads(test)
df = pd.json_normalize(data['geomagnetic-field-model-result'])