简体   繁体   中英

How do you convert a multiple arrays of string inside json into csv using python?

How do you convert a nested json of multiple arrays into csv tabular structure using python? see the complete json here

CODE :

import json
import csv

f = open('cost_drilldown_data.json')

data = json.load(f)
s=csv.writer(open('costdrillwittime_storage4.csv','w'))
s.writerow(["filter","cost","value","cost","subvalue","cost","res_id","cost","tot_cost","metdata"])
i=0
d = []
for breakdown in data['breakdown']:
    #for time in data['time']:
        for storage in data['storage']:
             if storage not in d:

                for values in breakdown['values']:

                    if 'subvalues' in values:
                         for subvalues in values['subvalues']:
                    #for i in range(0,len(data)):
                            s.writerow([breakdown['filter'],breakdown["cost"],values['value'],values['cost'],
                            subvalues["subvalue"],subvalues["cost"],storage['resource_id'],storage['cost'],
                            storage['total_cost'],storage['metadata']])

                    else :
                         s.writerow([breakdown['filter'],"","",values['value'],values['cost']])

ACTUAL OUTPUT OF THE ABOVE CODE:

filter,cost,value,cost,subvalue,cost,res_id,cost,tot_cost,metdata

tags,5517.734,Name,462.62,BizOps-VM-20,227.576,i-048e0bfa74ac9cf78,25.047,25.801000000000002,{u'name': u'BizOps0424001'}

tags,5517.734,Name,462.62,,70.358,i-048e0bfa74ac9cf78,25.047,25.801000000000002,{u'name': u'BizOps0424001'}

tags,5517.734,Name,462.62,BizOps01,60.188,i-048e0bfa74ac9cf78,25.047,25.801000000000002,{u'name': u'BizOps0424001'}...

its repeating the values of meta data again and again and also repeating the NAME after wards.

how to make print the values only once and differnt values only as per the counts.

see the full csv file here

TOTALLY EDITED CODE AFTER ANKUSH's HELP:

import json
import csv

f = open('cost_drilldown_data.json')
data = json.load(f)


s=csv.writer(open('googletry1.csv','w'))


breakdown = data['breakdown']
storage = data['storage']
filter_list = []            #first column
filter_cost_list = []       #second column
value_list = []             #third column
cost_list = []              #fourth column
subValue_list = []          #fifth col
subvalueCost_list = []      #sixth col
resID_list = []             #seventh col
storageCost_list = []       #eighth col
totalCost_list = []         #ninth col
metadata_list = []          #tenth col

s.writerow(["filter_list","filter_cost_list","value_list","cost_list","subValue_list","subvalueCost_list","resID_list","storageCost_list","totalCost_list","metadata_list"])

for eachBreakdown in breakdown:
    filter_list.append(eachBreakdown['filter'])
    filter_cost_list.append(['cost'])
    valuesArr = eachBreakdown['values']
    for eachValues in valuesArr:
        value_list.append(eachValues['value'])
        cost_list.append(eachValues['cost'])
        if 'subvalues' in eachValues:
            subValueArr = eachValues['subvalues']
        for eachSubValueArr in subValueArr:
            subValue_list.append(eachSubValueArr['subvalue'])
            subvalueCost_list.append(eachSubValueArr['cost'])

                    #s.writerow([breakdown['filter'],breakdown["cost"],values['value'],values['cost'],
                    #subvalues["subvalue"],subvalues["cost"],storage['resource_id'],storage['cost'],
                    #storage['total_cost'],storage['metadata'],storage['volume_cost'],storage['provider']])

            s.writerow([eachBreakdown['filter'],eachBreakdown['cost'],eachValues['value'],eachValues['cost'],
            eachSubValueArr['subvalue'],eachSubValueArr['cost']])

for eachStorage in storage:
    resID_list.append(eachStorage['resource_id'])
    storageCost_list.append(eachStorage['cost'])
    totalCost_list.append(eachStorage['total_cost'])
    metadata_list.append([eachStorage['metadata']])


    s.writerow([eachStorage['resource_id']])


now i am getting challenge in printing the resource id to the side of the subvalue cost: I am getting csv file as :
enter code here

The problem you posted in not clear, though I have tried to understand it. In the json you are working on, the objects are irregularly structured. What i mean is, there are not the same number of objects in the json for every key element. You also never mentioned what exactly you want in case of a data miss.

Find the code below. I have managed to get all the values of columns in a list. You may print them to see. Happy to help.

import json

f = open('cost_drilldown_data.json')
data = json.load(f)

breakdown = data['breakdown']
storage = data['storage']
filter_list = []            #first column
filter_cost_list = []       #second column
value_list = []             #third column
cost_list = []              #fourth column
subValue_list = []          #fifth col
subvalueCost_list = []      #sixth col
resID_list = []             #seventh col
storageCost_list = []       #eighth col
totalCost_list = []         #ninth col
metadata_list = []          #tenth col

for eachBreakdown in breakdown:
    filter_list.append(eachBreakdown['filter'])
    filter_cost_list.append(['cost'])
    valuesArr = eachBreakdown['values']
    for eachValues in valuesArr:
        value_list.append(eachValues['value'])
        cost_list.append(eachValues['cost'])
        if 'subvalues' in eachValues:
            subValueArr = eachValues['subvalues']
        for eachSubValueArr in subValueArr:
            subValue_list.append(eachSubValueArr['subvalue'])
            subvalueCost_list.append(eachSubValueArr['cost'])

for eachStorage in storage:
    resID_list.append(eachStorage['resource_id'])
    storageCost_list.append(eachStorage['cost'])
    totalCost_list.append(eachStorage['total_cost'])
    metadata_list.append([eachStorage['metadata']])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM