Python Forum
Print JSON Dictionary to Excel?
Thread Rating:
  • 2 Vote(s) - 4.5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Print JSON Dictionary to Excel?
#1
I would like to navigate through a complex JSON Dictionary and format it to be usable in an Excel file. I am fairly new to Python I've only been working with it for a couple of days.

import requests
import json
from pprint import pprint
import openpyxl
from openpyxl import Workbook

wb = Workbook()
ws = wb.active
dest_filename = 'MyTestSheet.xlsx'
ws.title = "MyTestSheet"

data_url = ''
response = requests.get(data_url)
response.raise_for_status()
data = response.json()

x = 1
y = 1

for i in range(0,100):
	ws.cell(row=x, column=y, value=str(data['features'][i]['attributes']))
	x = x+1

wb.save('MyTestSheet.xlsx')
What I want to do is print each item in the 'attributes' dictionary to its own row and then move to the next column and do the same for the next 'attributes' in 'features'. I know I can access the individual 'attributes' using the index like below.

data['features'][0]['attributes']
What I don't know how to do is how to loop through each string in the 'attributes' dictionary and print it to its own row in excel.

data['features'][0]['attributes'][0]
Above doesn't work and I think I know why its because:

type(data['features'])
Out[71]: list

type(data['features'][0]['attributes'])
Out[72]: dict

Additionally, I don't want to have to define a range for the index i = 0 to i = 100, coming from Excel VBA I am looking for something like usedrange.rows.count which essentially returns a count of the number of rows with data in them. That way my ranges could be dynamic if I use the same code with a different URL.

Think Think Think

The current code works but the output is crappy and I could do some excel manipulation to clean it up but why not shoot for a solution where python does it for me?

I really Heart the way pprint() makes it look.

OUCH I CANT POST CLICKABLE LINKS Wall

data_url = 'x'

x has been deleted so I can post the link
Reply


Messages In This Thread
Print JSON Dictionary to Excel? - by SimpleGuy - Aug-17-2017, 02:58 PM
RE: Print JSON Dictionary to Excel? - by nilamo - Aug-17-2017, 05:11 PM
RE: Print JSON Dictionary to Excel? - by SimpleGuy - Aug-17-2017, 05:30 PM
RE: Print JSON Dictionary to Excel? - by nilamo - Aug-17-2017, 05:43 PM
RE: Print JSON Dictionary to Excel? - by Larz60+ - Aug-17-2017, 11:09 PM
RE: Print JSON Dictionary to Excel? - by nilamo - Aug-18-2017, 04:10 AM
RE: Print JSON Dictionary to Excel? - by Larz60+ - Aug-18-2017, 01:05 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Converting cells in excel to JSON format desmondtay 4 3,147 May-23-2022, 10:31 AM
Last Post: Larz60+
Question How do I skipkeys on json file read to python dictionary? BrandonKastning 3 2,909 Mar-08-2022, 09:34 PM
Last Post: BrandonKastning
Question How to print each possible permutation in a dictionary that has arrays as values? noahverner1995 2 2,863 Dec-27-2021, 03:43 AM
Last Post: noahverner1995
  excel - json jmabrito 23 10,714 Mar-02-2021, 10:11 PM
Last Post: jmabrito
  saving a dictionary as json file vinay_py 6 4,648 Jun-06-2020, 05:07 PM
Last Post: vinay_py
  iterate and print json datas enigma619 1 2,503 Apr-01-2020, 11:48 AM
Last Post: buran
  print python json dump onto multiple lines lhailey 2 24,494 Mar-02-2020, 12:47 PM
Last Post: vishalhule
  accessing Json dictionary lshankar 1 2,493 Dec-16-2019, 06:18 AM
Last Post: ndc85430
  [split] Print JSON Dictionary to Excel? venukommu 1 3,063 Nov-15-2019, 09:33 PM
Last Post: micseydel
  Exporting list with dictionary to Excel veromi22 0 3,971 Oct-15-2019, 12:54 AM
Last Post: veromi22

Forum Jump:

User Panel Messages

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