function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Anil SavaliyaAnil Savaliya 

Json Response to Csv conversation

Hi Guys,

I am implementing API with our system,After Rest API  call i getting JSON response,Please see in example for json response,i need to convert JSON string in CSV file and need export csv, How can i implement ? Any Idea ? sample code ?
Response : 
[
    {
        "Revenue": "$1,000,000",
        "Employees": "12345",
        "MarketCap": "$1,000,000",
        "Website": "www.yahoo.com",
        "WebsiteURL": "http://www.yahoo.com",
        "Industry": "Information Technology",
        "LongDescription": "Long text",
        "Description": "Discripting here",
        "NodeID": "Org|345222",
        "DisplayName": "Yahoo News"
    },
    {
        "Revenue": "$2,000,000",
        "Employees": "345435",
        "MarketCap": "$1,02300,000",
        "Website": "www.google.com",
        "WebsiteURL": "http://www.google.com",
        "Industry": "Information Technology",
        "LongDescription": "Long text",
        "Description": "Discripting here",
        "NodeID": "Org|5323232",
        "DisplayName": "Google Search"
    },
    {
        "Revenue": "$5000,000,000",
        "Employees": "500000",
        "MarketCap": "$2,000,0000000",
        "Website": "www.apple.com",
        "WebsiteURL": "http://www.apple.com",
        "Industry": "Telecommunication",
        "LongDescription": "Long text",
        "Description": "Discripting here",
        "NodeID": "Org|534534",
        "DisplayName": "Apple News"
    }
]
pconpcon
This should get you what you want.  It's not really tested so it may not work well for large json sets or json that is missing elements.
 
String s = '[{"Revenue": "$1,000,000","Employees": "12345","MarketCap": "$1,000,000","Website": "www.yahoo.com","WebsiteURL": "http://www.yahoo.com","Industry": "Information Technology","LongDescription": "Long text","Description": "Discripting here","NodeID": "Org|345222","DisplayName": "Yahoo News"},{"Revenue": "$2,000,000","Employees": "345435","MarketCap": "$1,02300,000","Website": "www.google.com","WebsiteURL": "http://www.google.com","Industry": "Information Technology","LongDescription": "Long text","Description": "Discripting here","NodeID": "Org|5323232","DisplayName": "Google Search"},{"Revenue": "$5000,000,000","Employees": "500000","MarketCap": "$2,000,0000000","Website": "www.apple.com","WebsiteURL": "http://www.apple.com","Industry": "Telecommunication","LongDescription": "Long text","Description": "Discripting here","NodeID": "Org|534534","DisplayName": "Apple News"}]';

List<Object> entryList = (List<Object>) JSON.deserializeUntyped(s);

List<Map<String, Object>> mapList = new List<Map<String, Object>>();
Set<String> keySet = new Set<String>();

for (Object entry : entryList) {
    Map<String, Object> m = (Map<String, Object>)(entry);
    keySet.addAll(m.keySet());
    mapList.add(m);
}

List<String> keys = new List<String>(keySet);
keys.sort();

List<List<String>> csvLines = new List<List<String>>();

for (Integer i = 0; i <= mapList.size(); i++) {
    csvLines.add(new List<String>());
}

for (String key : keys) {
    csvLines.get(0).add('"' + key + '"');
    
    for (Integer i = 1; i <= mapList.size(); i++) {
        csvLines.get(i).add('"' + (String)(mapList.get(i - 1).get(key)) + '"');
    }
}

String csvFile = '';
for (List<String> csvLine : csvLines) {
    String line = '';
    for (Integer i = 0; i < csvLine.size() - 1; i++) {
    	line += csvLine.get(i) + ',';
    }
    line += csvLine.get(csvLine.size() - 1);
    csvFile += line + '\n';
}