Dynamically flatten json to csv from Salesforce?

I need to be able to read any object in Salesforce and write it out as a CSV file. To do this I need to be able to flatten the output dynamically.

Sample json:

[{
		"Account_Class_vod__c" : "abc",
		"ShippingAddress" : {
			"city" : "aaa",
			"country" : "Russia",
			"countryCode" : null,
			"geocodeAccuracy" : null,
			"latitude" : null,
			"longitude" : null,
			"postalCode" : "306200",
			"state" : "sss",
			"stateCode" : null,
			"street" : "st"
		},
		"Ownership" : "N"
	}, {
		"Account_Class_vod__c" : "abc",
		"ShippingAddress" : {
			"city" : "aaa",
			"country" : "Russia",
			"countryCode" : null,
			"geocodeAccuracy" : null,
			"latitude" : null,
			"longitude" : null,
			"postalCode" : "306200",
			"state" : "sss",
			"stateCode" : null,
			"street" : "st"
		},
		"Ownership" : "N"
	}, {
		"Account_Class_vod__c" : "abc",
		"ShippingAddress" : {
			"city" : "aaa",
			"country" : "Russia",
			"countryCode" : null,
			"geocodeAccuracy" : null,
			"latitude" : null,
			"longitude" : null,
			"postalCode" : "306200",
			"state" : "sss",
			"stateCode" : null,
			"street" : "st"
		},
		"Ownership" : "N"
	}
]

You can use mapper to change structure. Something like below:

I did changes for 2 fields only

Thank you for the mapper but my need is to do this dynamically. The data coming in to the mapper will not always be the same format. I’ll need to do this for a possible 850 salesforce objects all with different column names. I was thinking that a script snap would work but I’m not that strong in javascript, python, etc.

I am not following when you say dynamically.

Can you please elaborate with example.

The pipeline starts by listing all objects in salesforce and creates a SOQL statement for each object. The output from each object is different from the SOQL snap. In the above example data the Address field is not flat but in the second SOQL statement that runs could have 3 different fields that need to be flattened.

We solve this by using a python script. I shared this under “Showcase” section. You can locate the “flattener” python script under Utilties / Collection of Python Scripts.

1 Like

Exactly what I was looking for! Thanks!

I cant find this script. Can you please guide me to correct location for this script.

Surya, if you check out the post here, Alan links to a box folder with the resource you are looking for.