Python Forum
How to save json data in a dataframe
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to save json data in a dataframe
#1
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))
Reply
#2
data = json.loads(test)
df = pd.json_normalize(data['geomagnetic-field-model-result'])
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Reading data to python: turn into list or dataframe hhchenfx 2 166 Jun-01-2021, 10:28 AM
Last Post: Larz60+
  JSON Decode error when using API to create dataframe Rubstiano7 4 469 Jan-11-2021, 07:52 PM
Last Post: buran
  Sort data from JSON file Dummy_in_programming 2 430 Jan-04-2021, 06:17 PM
Last Post: deanhystad
  Json File more pages #pandas #dataframe nio74maz 0 380 Dec-30-2020, 05:32 AM
Last Post: nio74maz
  Pandas Extract data from two dataframe nio74maz 1 384 Dec-26-2020, 09:52 PM
Last Post: nio74maz
  pyspark dataframe to json without header vijz 0 328 Nov-28-2020, 05:36 PM
Last Post: vijz
  sqlite3 database does not save data across restarting the program SheeppOSU 1 743 Jul-24-2020, 05:53 AM
Last Post: SheeppOSU
  How to save Python Requests data sent to server? RedLeonard 5 1,242 Jul-05-2020, 10:33 AM
Last Post: RedLeonard
  How to save CSV file data into the Azure Data Lake Storage Gen2 table? Mangesh121 0 623 Jun-26-2020, 11:59 AM
Last Post: Mangesh121
  Save Arduino data in mysql server via raspberrypi rithikvg 1 899 Jun-24-2020, 10:59 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020