choicevur.blogg.se

Convert nested json to csv python
Convert nested json to csv python










convert nested json to csv python

  • the values of those keys both happen to also be dictionaries.
  • a dictionary that consists of 2 keys (with names copyright and teams),.
  • The structure of the data here and it’s formatting is very simple from python3 programmer’s point of view (Although, I admit it was a bit of a confusing reply to read… )Īs for the moment, I have only completed Python3 course in 66%, so there could be a more optimized way to do this, but just using what I already learned I was able to accomplish all the described results.įirst things first - I think there is no other way to build a code that works with JSON file from API without analyzing the structure of the reply. I’ve read through the topic and indeed is correct about the fact that this JSON response is basically a dictionary with some keys being assigned value of other dictionaries and this goes a couple of “ dimentios” down. With open('output.csv', 'w', newline='') as f_output:Ĭsv_output = csv.DictWriter(f_output, fieldnames=sorted(fieldnames))Ĭsv_output.writerows(get_leaves(entry) for entry in json_data) # First parse all entries to get the complete fieldname listįieldnames.update(get_leaves(entry).keys()) Here is the code that should work apart from not being able to have a CSV friendly dict. Notice how the first team did not have the “Venue.ID” which makes things even more difficult. Id, name, link, venue_id, venue_name, venue_link…ġ, New Jersey Devils, /api/v1/teams/1, Prudential Center, /api/v1/venues/nullĢ, New York Islanders, /api/v1/teams/2, 5026, Barclays Center, /api/v1/venues/5026

    convert nested json to csv python

    The desired result would be to like this (this is a shortened version): “copyright” (Cell A2) and “teams” (A3) so it doesnt flatten the dict as I want it to.

    convert nested json to csv python

    However, the CSV that I get only has two cells. I have Googled this quite a bit and the code for flattening the json/dict is from this post. However, I got stuck trying to get the first JSON response to a CSV for further analysis. Since the NHL season is about to start I am planning on downloading some stats and team info through the NHL API.












    Convert nested json to csv python