JSON to CSV Convert – Utility Class for Apex and Java

While converting you CSV to JSON can be easy, converting JSON to CSV is definitely not an easy task.

JSON is not meant to be converted to CSV, as it is key-value pair based language where a value can be anything, it can be a simple string or it can be a complex object or it can be an array. While CSV is the complete opposite, its structure data in column and rows.

While converting JSON to CSV doesn’t make much sense, you can still get the business requirement to do so for the following reasons:

  • JSON is not meant to be read by human eyes. CSV is easy to read and understand.
  • Editing JSON is not easy and you can damage the JSON structure while editing it where CSV is very easy to edit.
  • Reporting on JSON file is not easy, CSV is far better in reporting as it is structured data.
  • JSON is meant for computers and programs, it works best when you use it inside your application, where it can be generated and parsed by the application itself, but if you need a JSON file as output to distribute to business users, it won’t make much sense.
Enough Talk, Show Us The Code

This was one of the most complex tasks that I have done till date. I have created below class, which can accept a JSON String and you will a CSV String in return. The same algorithm can be used in any programming language like Java, JavaScript, C# etc.




Also Read: Calculate Business Days In Apex – Utility Class

I have tested above code with some complex JSON files, and I have got good results. You need to call getCSVString() methos and supply your JSON string. If you find that the code is not working as desired, please comment on this post with your question.

(Visited 681 Times)

1 thought on “JSON to CSV Convert – Utility Class for Apex and Java

  1. Himanshu

    Could you please Try this below JSON structure. This is perfect JSON format but your code is giving some error. Also let me know what we will do after getting CSV string ?

    [{“attributes”:{“type”:”Support_Request__c”,”url”:”/services/data/v44.0/sobjects/Support_Request__c/a0R41000007ovfmEAA”},”Integrated_with_IMN__c”:false,”Date_Time_Submitted__c”:”2018-11-27T15:07:12.000+0000″,”OwnerId”:”00541000001NNM5AAO”,”CreatedById”:”00541000001NNM5AAO”,”LastModifiedById”:”00541000004Mz8fAAC”,”CreatedDate”:”2018-05-09T14:31:43.000+0000″,”Production_Budget__c”:0.00,”Ticket_Number__c”:”SR-180509-21182″,”Advertiser__c”:”0014100000gkNpDAAU”,”RecordTypeId”:”01241000000k2zOAAQ”,”Type_of_Request__c”:”Post Sale Production Request”,”Total_Production_Value__c”:0.00,”Payment_Terms_Formula__c”:”Cash In Advance”,”Co_Op_Formula__c”:true,”Advertiser_Locations__c”:”NA\r\nNA, NA 59601\r\nUSA”,”New_Business__c”:false,”TotalProductionAmounttobeBilled__c”:0.00,”Commodity_Code_Formula__c”:”ADV_MKG — Marketing”,”Account_Executive__c”:”00541000001NNM5AAO”,”Division__c”:”Central”,”Region__c”:”MidWest”,”Sales_Office__c”:”Corporate Ops”,”Agency__c”:”0014100001K2KeOAAV”,”Request_Description_Production__c”:”TEST FOR TRAINING”,”LastModifiedDate”:”2018-11-27T15:07:12.000+0000″,”Due_Date__c”:”2018-05-30″,”Status__c”:”Submitted to Kernel”,”Name”:”Full Production”,”Id”:”a0R41000007ovfmEAA”,”Production_Services__r”:{“totalSize”:2,”done”:true,”records”:[{“attributes”:{“type”:”Production_Service__c”,”url”:”/services/data/v44.0/sobjects/Production_Service__c/a0J4100000IFrTUEA1″},”Support_Request__c”:”a0R41000007ovfmEAA”,”Id”:”a0J4100000IFrTUEA1″,”IMN_ID__c”:”6785″,”CreatedDate”:”2018-05-09T14:35:01.000+0000″,”LastModifiedDate”:”2018-11-14T18:31:26.000+0000″,”Integrated_with_IMN__c”:true,”Flight_Start_Date__c”:”2018-06-04″,”LastModifiedById”:”005m0000003yPNhAAM”,”RecordTypeId”:”01241000000k2zFAAQ”,”CreatedById”:”00541000001NNM5AAO”,”Account_Executive__c”:”00541000001NNM5AAO”,”Name”:”Pre Sale Spot Summer 2018″,”Comments_Special_instructions__c”:”client wants to have his dog in the spot”,”Billed__c”:false,”Support_Request__r”:{“attributes”:{“type”:”Support_Request__c”,”url”:”/services/data/v44.0/sobjects/Support_Request__c/a0R41000007ovfmEAA”},”Id”:”a0R41000007ovfmEAA”,”Sales_Office__c”:”Corporate Ops”,”Region__c”:”MidWest”,”Division__c”:”Central”,”Advertiser__c”:”0014100000gkNpDAAU”,”Name”:”Full Production”,”Account_Executive__c”:”00541000001NNM5AAO”,”Advertiser__r”:{“attributes”:{“type”:”Account”,”url”:”/services/data/v44.0/sobjects/Account/0014100000gkNpDAAU”},”Id”:”0014100000gkNpDAAU”,”Name”:”Glacier National Park TESTER”},”Account_Executive__r”:{“attributes”:{“type”:”User”,”url”:”/services/data/v44.0/sobjects/User/00541000001NNM5AAO”},”Id”:”00541000001NNM5AAO”,”Name”:”Greg Rice”}},”LastModifiedBy”:{“attributes”:{“type”:”User”,”url”:”/services/data/v44.0/sobjects/User/005m0000003yPNhAAM”},”Id”:”005m0000003yPNhAAM”,”Name”:”Boomi kernel”},”RecordType”:{“attributes”:{“type”:”RecordType”,”url”:”/services/data/v44.0/sobjects/RecordType/01241000000k2zFAAQ”},”Id”:”01241000000k2zFAAQ”,”Name”:”Linear – New :30 spot”},”CreatedBy”:{“attributes”:{“type”:”User”,”url”:”/services/data/v44.0/sobjects/User/00541000001NNM5AAO”},”Id”:”00541000001NNM5AAO”,”Name”:”Greg Rice”},”Account_Executive__r”:{“attributes”:{“type”:”User”,”url”:”/services/data/v44.0/sobjects/User/00541000001NNM5AAO”},”Id”:”00541000001NNM5AAO”,”Name”:”Greg Rice”}},{“attributes”:{“type”:”Production_Service__c”,”url”:”/services/data/v44.0/sobjects/Production_Service__c/a0J4100000IFrTZEA1″},”Support_Request__c”:”a0R41000007ovfmEAA”,”Id”:”a0J4100000IFrTZEA1″,”IMN_ID__c”:”6787″,”CreatedDate”:”2018-05-09T14:35:43.000+0000″,”LastModifiedDate”:”2018-11-14T18:31:20.000+0000″,”Integrated_with_IMN__c”:true,”Flight_Start_Date__c”:”2018-06-18″,”LastModifiedById”:”005m0000003yPNhAAM”,”RecordTypeId”:”01241000000k2zFAAQ”,”CreatedById”:”00541000001NNM5AAO”,”Account_Executive__c”:”00541000001NNM5AAO”,”Name”:”On Sale Now Summer 2018″,”Comments_Special_instructions__c”:”TEST FOR TRAINING”,”Billed__c”:false,”Support_Request__r”:{“attributes”:{“type”:”Support_Request__c”,”url”:”/services/data/v44.0/sobjects/Support_Request__c/a0R41000007ovfmEAA”},”Id”:”a0R41000007ovfmEAA”,”Sales_Office__c”:”Corporate Ops”,”Region__c”:”MidWest”,”Division__c”:”Central”,”Advertiser__c”:”0014100000gkNpDAAU”,”Name”:”Full Production”,”Account_Executive__c”:”00541000001NNM5AAO”,”Advertiser__r”:{“attributes”:{“type”:”Account”,”url”:”/services/data/v44.0/sobjects/Account/0014100000gkNpDAAU”},”Id”:”0014100000gkNpDAAU”,”Name”:”Glacier National Park TESTER”},”Account_Executive__r”:{“attributes”:{“type”:”User”,”url”:”/services/data/v44.0/sobjects/User/00541000001NNM5AAO”},”Id”:”00541000001NNM5AAO”,”Name”:”Greg Rice”}},”LastModifiedBy”:{“attributes”:{“type”:”User”,”url”:”/services/data/v44.0/sobjects/User/005m0000003yPNhAAM”},”Id”:”005m0000003yPNhAAM”,”Name”:”Boomi kernel”},”RecordType”:{“attributes”:{“type”:”RecordType”,”url”:”/services/data/v44.0/sobjects/RecordType/01241000000k2zFAAQ”},”Id”:”01241000000k2zFAAQ”,”Name”:”Linear – New :30 spot”},”CreatedBy”:{“attributes”:{“type”:”User”,”url”:”/services/data/v44.0/sobjects/User/00541000001NNM5AAO”},”Id”:”00541000001NNM5AAO”,”Name”:”Greg Rice”},”Account_Executive__r”:{“attributes”:{“type”:”User”,”url”:”/services/data/v44.0/sobjects/User/00541000001NNM5AAO”},”Id”:”00541000001NNM5AAO”,”Name”:”Greg Rice”}}]},”Owner”:{“attributes”:{“type”:”Name”,”url”:”/services/data/v44.0/sobjects/User/00541000001NNM5AAO”},”Id”:”00541000001NNM5AAO”,”Name”:”Greg Rice”},”CreatedBy”:{“attributes”:{“type”:”User”,”url”:”/services/data/v44.0/sobjects/User/00541000001NNM5AAO”},”Id”:”00541000001NNM5AAO”,”Name”:”Greg Rice”},”LastModifiedBy”:{“attributes”:{“type”:”User”,”url”:”/services/data/v44.0/sobjects/User/00541000004Mz8fAAC”},”Id”:”00541000004Mz8fAAC”,”Name”:”Vinay Chaturvedi”},”Advertiser__r”:{“attributes”:{“type”:”Account”,”url”:”/services/data/v44.0/sobjects/Account/0014100000gkNpDAAU”},”Id”:”0014100000gkNpDAAU”,”Name”:”Glacier National Park TESTER”},”RecordType”:{“attributes”:{“type”:”RecordType”,”url”:”/services/data/v44.0/sobjects/RecordType/01241000000k2zOAAQ”},”Id”:”01241000000k2zOAAQ”,”Name”:”Production”},”Account_Executive__r”:{“attributes”:{“type”:”User”,”url”:”/services/data/v44.0/sobjects/User/00541000001NNM5AAO”},”Id”:”00541000001NNM5AAO”,”Name”:”Greg Rice”},”Agency__r”:{“attributes”:{“type”:”Account”,”url”:”/services/data/v44.0/sobjects/Account/0014100001K2KeOAAV”},”Id”:”0014100001K2KeOAAV”,”Name”:”Test Agency”}}]

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *