2021年2月11日星期四

Python JSON String to tabular formatting

The script at the bottom of the post helps me fetch and re-orient the data from JSON into a list of info like shown directly below...

I need to restructure this into tabular format for either CSV conversion, or better yet do a MySQL INSERT after its been restructured to X over Y axis's, however the current JSON output has re-occurring headers that are not needed in a conversion to table, so I'm really just trying to strip the response of say "first_name" and "last_name" values, and have this as column headers while the values are denoted in rows.

Not super savvy yet with Python, but I feel its the most sensible way to handle the processing, any help appreciated!

id , 9999999  first_name , Joe  last_name , Schmo  group_id , 99999  active , True  employee_number , 0  salaried , False  exempt , False  username , jschmo  email , abc123@gmail.com  email_verified , False  payroll_id ,  mobile_number , 9999999999  hire_date , 0000-00-00  term_date , 0000-00-00  last_modified , 2021-02-08T01:59:42+00:00  last_active , 2021-02-10T22:45:34+00:00  created , 2021-02-04T20:07:38+00:00  client_url , abcefg  company_name , Some Company Name  profile_image_url , https://www.gravatar.com/avatar/000000000000000000000000000  display_name ,  pronouns ,  pto_balances,  1400406 , 0  submitted_to , 2021-02-08  approved_to , 2021-02-08  manager_of_group_ids , []  require_password_change , False  pay_rate , 0  pay_interval , hour  permissions,  admin , False  mobile , True  status_box , False  reports , False  manage_timesheets , False  manage_authorization , False  manage_users , False  manage_my_timesheets , False  manage_jobcodes , False  pin_login , True  approve_timesheets , False  manage_schedules , False  external_access , False  manage_my_schedule , False  manage_company_schedules , False  view_company_schedules , False  view_group_schedules , False  manage_no_schedules , False  view_my_schedules , False  view_projects , False  manage_projects , False  time_tracking , True  customfields ,  id , 1111111  first_name , Jane  last_name , Doe  group_id , 11111  active , True  employee_number , 0  salaried , False  exempt , False  username , jdoe  email , xyz789@gmail.com  email_verified , False  payroll_id ,  mobile_number , 111111111  hire_date , 0000-00-00  term_date , 0000-00-00  last_modified , 2021-02-08T01:59:42+00:00  last_active , 2021-02-10T22:45:34+00:00  created , 2021-02-04T20:07:38+00:00  client_url , abcefg  company_name , Some Company Name  profile_image_url , https://www.gravatar.com/avatar/000000000000000000000000000  display_name ,  pronouns ,  pto_balances,  1400406 , 0  submitted_to , 2021-02-08  approved_to , 2021-02-08  manager_of_group_ids , []  require_password_change , False  pay_rate , 0  pay_interval , hour  permissions,  admin , False  mobile , True  status_box , False  reports , False  manage_timesheets , False  manage_authorization , False  manage_users , False  manage_my_timesheets , False  manage_jobcodes , False  pin_login , True  approve_timesheets , False  manage_schedules , False  external_access , False  manage_my_schedule , False  manage_company_schedules , False  view_company_schedules , False  view_group_schedules , False  manage_no_schedules , False  view_my_schedules , False  view_projects , False  manage_projects , False  time_tracking , True  customfields ,  
import requests  import json  import csv        url = "https://rest.apiofsomesort.com/api/v1/users"    payload = ""  headers = {     'Authorization': "Bearer                ",    }    response = requests.request("GET", url, data=payload, headers=headers)    #print(response.text)    # Saves response to JSON file  emp_data = response.json()  with open('emp_data.json', 'w') as f:      json.dump(emp_data, f)    # Loads from JSON file while iterating recursively through nested keys,values  with open('emp_data.json','r') as string:      my_dict=json.load(string)  string.close()  def iterate_emp_data(my_dict):      for k,v in my_dict.items():          if(isinstance(v,dict)):              print(k+",")              iterate_emp_data(v)              continue          print(k+" , "+str(v))  iterate_emp_data(my_dict)  
https://stackoverflow.com/questions/66165129/python-json-string-to-tabular-formatting February 12, 2021 at 08:59AM

没有评论:

发表评论