Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

convert nested json to csv python

import json
import csv

def get_leaves(item, key=None):
    if isinstance(item, dict):
        leaves = []
        for i in item.keys():
            leaves.extend(get_leaves(item[i], i))
        return leaves
    elif isinstance(item, list):
        leaves = []
        for i in item:
            leaves.extend(get_leaves(i, key))
        return leaves
    else:
        return [(key, item)]


with open('json.txt') as f_input, open('output.csv', 'w', newline='') as f_output:
    csv_output = csv.writer(f_output)
    write_header = True

    for entry in json.load(f_input):
        leaf_entries = sorted(get_leaves(entry))

        if write_header:
            csv_output.writerow([k for k, v in leaf_entries])
            write_header = False

        csv_output.writerow([v for k, v in leaf_entries])
Comment

PREVIOUS NEXT
Code Example
Javascript :: multiple queries in node js 
Javascript :: react native flex 2 columns per row 
Javascript :: discord.js reason 
Javascript :: react cors error 
Javascript :: javascript debugging exercises 
Javascript :: convert array to csv javascript 
Javascript :: react particles 
Javascript :: typeof date 
Javascript :: angular countdown begin stop pause 
Javascript :: [Homepage] is not a <Route component. All component children of <Routes must be a <Route or <React.Fragment 
Javascript :: get url 
Javascript :: how to prevent xss attacks in node js 
Javascript :: react class components 
Javascript :: typeof in js 
Javascript :: initalise typed js library 
Javascript :: how to use json stringify in javascript 
Javascript :: sort() object values javascript 
Javascript :: jquery vertical scroll 
Javascript :: tables javascript 
Javascript :: what is asynchronous 
Javascript :: creatable select react 
Javascript :: how to set selected value of dropdown in javascript 
Javascript :: for i in range vue js 
Javascript :: why does javascript have hoisting 
Javascript :: what does = mean in javascript 
Javascript :: component will unmount 
Javascript :: redux toolkit remove from array 
Javascript :: array flatten 
Javascript :: array remove last item 
Javascript :: what regular expression will match valid international phone numbers 
ADD CONTENT
Topic
Content
Source link
Name
8+6 =