0

In the below JSON I need to put all the fields under customer and address as the column headers and then all the values for the fields.

{
  "customer" : {
    "customerReferenceId" : "string",
    "customerName" : "string",
    "customerId" : "string",
    "lastFourTaxId" : "string",
    "primaryOfficerId" : "string",
    "primaryOfficerName" : "string"
  },
  "address" : {
    "addressType" : "string",
    "addressLine1" : "string",
    "addressLine2" : "string",
    "addressLine3" : "string",
    "addressLine4" : "string",
    "city" : "string",
    "stateCode" : "string",
    "postalCode" : "string",
    "countryCode" : "string",
    "countryName" : "string"
  }
}

I have below code which works for me but that is only for customer, how do I put the fields under address as well as headers and then values under them.

import json
import csv

with open('c.json') as json_data:
      data = json.load(json_data)
      fo = open("CustomerSearch.csv","w")
      fo.write("CustomerId"+ "," + "CustomerName" + "," + 
              "CustomerReferenceId" + "," + "LastFourTaxId" + "," + 
              "PrimaryOfficerId" + "," + "PrimaryOfficerName")
      fo.write('\n')
      for r in data['customerData']:             
          fo.write(r ['customerId'] + "," + r['customerName'] + "," + 
                  r['customerReferenceId'] + "," + r['lastFourTaxId'] + "," 
                  + r['primaryOfficerId'] + "," + r['primaryOfficerName'])
          fo.write('\n')
      fo.close()    
ɢʀᴜɴᴛ
  • 32,025
  • 15
  • 116
  • 110

0 Answers0