0

I am using python to convert complex json into csv

              I want to convert complex json into csv using python      
               sample json :
                {
                    "data":[
               {
           "filter":"tags",
            "cost":1234,
          "values":[
           {
          "count":13,
         "subvalues":[
            {
              "count":1,
             "subvalue":"goat",
             "cost":227.576
             },
              "storage":[
                {
            "resource_id":"1234343413",
          "cost":25.047,
             "running_hours %":61.52777777777778,
           "created_in":"asd ",
             "account_name":null,
                "service":"csdsae",
                etcc...

I have written the code as below: my python code :

                        import json
                       import csv

                  f = open('file.json')

                 data = json.load(f)
                s=csv.writer(open('test6.csv','w'))
               count = 0
                for item in data["breakdown"]:
              s.writerow([item])

                if count == 0:
                    header = item.keys()

                    s.writerow(header)

                    count += 1

                    s.writerow(item.values())

But i am not getting proper output in csv file getting only one line but the complete json is not coming

  • First, your sample code is formatted so badly it is hard to make heads or tails of it. Second, you seem to have used `if` statement while meaning to use `while`. Third, it would be better to use `for` loop to iterate over your `item` anyway. – Błotosmętek Jun 16 '17 at 08:42
  • "values":[ { "count":13, "subvalues":[ { "count":1, "subvalue":"BizOps-VM-20", "cost":227.576 }, { "count":4, "subvalue":"", "cost":70.35 I tried something like : s.writerow(["filter","cost","values"]) for data in data["breakdown"]: s.writerow([data["filter"],data["cost"],data["values"]]) –  Jun 16 '17 at 09:30

0 Answers0