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
  encrypt data in json file help jacksfrustration 1 191 Mar-28-2024, 05:16 PM
Last Post: deanhystad
  how to save to multiple locations during save cubangt 1 543 Oct-23-2023, 10:16 PM
Last Post: deanhystad
  Filter data into new dataframe as main dataframe is being populated cubangt 8 996 Oct-23-2023, 12:43 AM
Last Post: cubangt
  Converting a json file to a dataframe with rows and columns eyavuz21 13 4,405 Jan-29-2023, 03:59 PM
Last Post: eyavuz21
  Read nested data from JSON - Getting an error marlonbown 5 1,358 Nov-23-2022, 03:51 PM
Last Post: snippsat
  Reading Data from JSON tpolim008 2 1,077 Sep-27-2022, 06:34 PM
Last Post: Larz60+
  Seeing al the data in a dataframe or numpy.array Led_Zeppelin 1 1,139 Jul-11-2022, 08:54 PM
Last Post: Larz60+
  Need help formatting dataframe data before saving to CSV cubangt 16 5,782 Jul-01-2022, 12:54 PM
Last Post: cubangt
  Convert nested sample json api data into csv in python shantanu97 3 2,808 May-21-2022, 01:30 PM
Last Post: deanhystad
  Struggling with Juggling JSON Data SamWatt 7 1,884 May-09-2022, 02:49 AM
Last Post: snippsat

Forum Jump:

User Panel Messages

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