Nested JSON to CSV Conversion
<p>I am receiving few JSON responses from REST APIs, format of my response is following :-</p>

<pre><code>{
 "headings": [
   "ACCOUNT_ID",
   "date",
   "FB Likes"
 ],
 "rows": [
   [
    "My Account",
    "1435708800000",
     117
   ],
   [
   "My Account",
   "1435795200000",
   99
   ],
   [
   "My Account",
   "1435708800000",
   7
  ]
]
}
</code></pre>

<p>Where columns are AccountID, Date and FB_Likes, and I am trying to convert it into csv, I have tried many different iterations but with no succees. </p>

<p>Please help me with this </p>

<p>One of my used scripts were</p>

<pre><code>with open('Account_Insights_12Jan.json') as fi:
data = json.load(fi)

 json_array=data

columns = set()
for item in json_array:
   columns.update(set(item))

# writing the data on csv
with open('Test_14Jan.csv', 'w', newline='') as fo:
writer = csv.writer(fo)

writer.writerow(list(columns))
for item in json_array:
    row = []
    for c in columns:
        if c in item: row.append(str(item[c]))
        else: row.append('')
    writer.writerow(row)
</code></pre>

<p>N I am receiving a error from it, I copied it from somewhere, Pls explain how to convert it </p>

<p>Hi Again</p>

<pre><code>{
 "headings": [
"POST_ ID",
"POST_COMMENT_COUNT"
 ],
 "rows": [
 [
  {
    "postId": 188365573,
    "messageId": 198365562,
    "accountId": 214,
    "messageType": 2,
    "channelType": "TWITTER",
    "accountType": "TWITTER",
    "taxonomy": {
      "campaignId": "2521_4",
      "clientCustomProperties": {
        "PromotionChannelAbbreviation": [
          "3tw"
        ],
        "PromotionChannels": [
          "Twitter"
        ],
        "ContentOwner": [
          "Audit"
        ],
        "Location": [
          "us"
        ],
        "Sub_Category": [
          "dbriefs"
        ],
        "ContentOwnerAbbreviation": [
          "aud"
        ],
        "PrimaryPurpose_Outcome": [
          "Engagement"
        ],
        "PrimaryPurposeOutcomeAbbv": [
          "eng"
        ]
      },
      "partnerCustomProperties": {},
      "tags": [],
      "urlShortnerDomain": "2721_spr.ly"
    },
    "approval": {
      "approvalOption": "NONE",
      "comment": ""
    },
    "status": "SENT",
    "createdDate": 1433331585000,
    "scheduleDate": 1435783440000,
    "version": 4,
    "deleted": false,
    "publishedDate": 1435783441000,
    "statusID": "6163465412728176",
    "permalink": "https://twitter.com/Acctg/status/916346541272498176",
    "additional": {
      "links": []
    }
  },
  0
],
[
  {
    "postId": 999145171,
    "messageId": 109145169,
    "accountId": 21388,
    "messageType": 2,
    "channelType": "TWITTER",
    "accountType": "TWITTER",
    "taxonomy": {
      "campaignId": "2521_4",
      "clientCustomProperties": {
        "PromotionChannelAbbreviation": [
          "3tw"
        ],
        "Eminence_Registry_Number": [
          "1000159"
        ],
        "PromotionChannels": [
          "Twitter"
        ],
        "ContentOwner": [
          "Ctr. Health Solutions"
        ],
        "Location": [
          "us"
        ],
        "Sub_Category": [
          "fraud"
        ],
        "ContentOwnerAbbreviation": [
          "chs"
        ],
        "PrimaryPurpose_Outcome": [
          "Awareness"
        ],
        "PrimaryPurposeOutcomeAbbv": [
          "awa"
        ]
      },
      "partnerCustomProperties": {},
      "tags": [],
      "urlShortnerDomain": "2521_spr.ly"
    },
    "approval": {
      "approvalOption": "NONE",
      "comment": ""
    },
    "status": "SENT",
    "createdDate": 1434983660000,
    "scheduleDate": 1435753800000,
    "version": 4,
    "deleted": false,
    "publishedDate": 1435753801000,
    "statusID": "616222222198407168",
    "permalink": "https://twitter.com/Health/status/6162222221984070968",
    "additional": {
      "links": []
    }
  },
  0
]   
}
</code></pre>

<p>Please consider this JSON response as well
Thanks Again fr all the help,  You are a savior!</p>

<p>Response will look Like following. It is a sample output since there are many columns, I am including few of them. My bad, I dont know how to share an excel output </p>

<p>Post ID,MessageID,AccountID,messageType,accountType,Channel Type<br>
188365573,198365562,214,2,Twitter,Twitter</p>

<hr>

<p>999145171,109145169,21388,2,Twitter,Twitter</p>

<p>The code in process is </p>

<pre><code>csvdata= open('Data_table2.csv', 'w')
csvwriter = csv.writer(csvdata, delimiter=',')
csvwriter.writerow(header)


for i in range(0,70):
  csvwriter.writerow(data1["rows"][i][0].values())

csvdata.close()
</code></pre>

<p>But not working successfully, because of many nested version and also, in some of the responses we have some headers which need to e checked and if it is not there then make a new header for that </p>

<p>Thanks Again for all the help!
Manu </p>