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
#2
Does it HAVE to be an excel document?  Excel will open csv just fine, and generating csv is much easier to think about.
Also, dicts don't have ordered keys, which means each feature's attributes might be in a different order.  So just writing them out in the order you find them might give wacky output.

Here's what I came up with.  It keeps track of which attribute-keys it's seen, then uses that to make sure all values go into the right columns.  Finally, it's all written to a csv file.  Also, I faked the json data so it can be run and tested:
import csv

# fake json data
data = {
    "features": [
        {
            "attributes": {
                "color": "green",
                "size": "5",
                "can_rotate": "no"
            }
        },
        {
            "attributes": {
                "can_rotate": "yes",
                "color": "black"
            }
        },
        {
            "attributes": {
                "color": "red",
                "size": "9"
            }
        }
    ]
}

columns = []
all_rows = []
for feature in data["features"]:
    row = ["" for col in columns]
    for key, value in feature["attributes"].items():
        try:
            index = columns.index(key)
        except ValueError:
            # this column hasn't been seen before
            columns.append(key)
            row.append("")
            index = len(columns) - 1

        row[index] = value
    all_rows.append(row)

with open("my-file.csv", "w") as csvfile:
    writer = csv.writer(csvfile)
    # first row is the headers
    writer.writerow(columns)
    # then, the rows
    writer.writerows(all_rows)
Reply
#3
This is great it works perfectly with my actual data. I will spend a little bit of time figuring out what you did and use it as a learning experience. Thank you!
Reply
#4
I don't know what the json looks like, but if all attributes have all the properties, it can be simplified quite a bit.  Most of what I wrote is just getting around possible missing attributes.

For example, if all the attributes always exist for each feature, it gets nearly simple:
with open("my-file.csv", "w") as csvfile:
    writer = csv.writer(csvfile)

    columns = [ ]
    for feature in data["features"]:
        if not columns:
            # add the headers
            columns = feature["attributes"].keys()
            writer.writerow(columns)

        row = [feature["attributes"][column] for column in columns]
        writer.writerow(row)
Reply
#5
FYI:
dictionaries are ordered as of version 3.6
Reply
#6
Ordered... predictably?  Or do they just maintain the order of the originating data?
If the source json had the keys in different orders, would dicts sort them to be the same, or just maintain the order they originally had?
Reply
#7
original order.

test:
def ziggy():
    unordered_dict = {
        'b': 'This is b',
        'h': 'This is h',
        'a': 'This is a',
        'c': 'This is c'
    }

    for key, value in unordered_dict.items():
        print('key: {} value: {}'.format(key, value))
results:
Output:
key: b value: This is b key: h value: This is h key: a value: This is a key: c value: This is c Process finished with exit code 0
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Converting cells in excel to JSON format desmondtay 4 1,727 May-23-2022, 10:31 AM
Last Post: Larz60+
Question How do I skipkeys on json file read to python dictionary? BrandonKastning 3 1,885 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 1,733 Dec-27-2021, 03:43 AM
Last Post: noahverner1995
  excel - json jmabrito 23 6,468 Mar-02-2021, 10:11 PM
Last Post: jmabrito
  saving a dictionary as json file vinay_py 6 3,086 Jun-06-2020, 05:07 PM
Last Post: vinay_py
  iterate and print json datas enigma619 1 1,920 Apr-01-2020, 11:48 AM
Last Post: buran
  print python json dump onto multiple lines lhailey 2 19,822 Mar-02-2020, 12:47 PM
Last Post: vishalhule
  accessing Json dictionary lshankar 1 1,839 Dec-16-2019, 06:18 AM
Last Post: ndc85430
  [split] Print JSON Dictionary to Excel? venukommu 1 2,291 Nov-15-2019, 09:33 PM
Last Post: micseydel
  Exporting list with dictionary to Excel veromi22 0 3,042 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