1

I am integration Salesforce with Heroku.I have create the sql query in Heroku Dataclip to get the JSON. My SQL query :

SELECT ent.tech_assessment_token AS Assess_TOKEN,ent.stage As stage, ent.created_at,ent.updated_at FROM entries ent limit 4;

The JSON create is not in Key:Value formmat,because of which its difficult for me to parse further in Rest Apex class. Following my JSON generated :

{"title":"Test Data Clip","fields":["assess_token","stage","created_at","updated_at"],"types":[1043,23,1114,1114],"type_names":["text","integer","unknown","unknown"],"values":[["a7063e277631887fc81e0ed9a3a437f5",6,"2016-08-17 16:46:22.615632","2016-09-28 19:13:00.94779"],["015e760aa9c5abf8a22a817f54ca248e",1,"2016-08-17 18:57:52.508419","2016-08-18 11:49:44.470628"],["83d9b617120d0299bc0e0c3ef0397752",6,"2016-08-17 17:08:17.087928","2016-09-28 19:15:31.672807"],["a83d6e8a97616697e94ce2bcaa6e73ba",0,"2016-08-17 19:35:31.356247","2016-09-13 13:26:58.707726"]]}

Is there any way to change the input JSON format in Heroku app itself? Or do I need to modify my parsing logic in Salesforce.

1

You can use json2apex to generate a file that you can use to parse your output. When I tried it, it came up with the following Apex Code:

//
// Generated by JSON2Apex http://json2apex.herokuapp.com/
//

public class JSON2Apex {

    public String title;
    public List<String> fields;
    public List<Integer> types;
    public List<String> type_names;
    public List<List<String>> values;


    public static JSON2Apex parse(String json) {
        return (JSON2Apex) System.JSON.deserialize(json, JSON2Apex.class);
    }
}

You can safely ignore "types", as it is not really useful as is, but by using "fields", "type_names" and "values", you can pretty easily parse out what's left:

List<Map<String, Object>> parsedObjects = new List<Map<String, Object>>();
for(String[] item: result.values) {
    Map<String, Object> itemData = new Map<String, Object>();
    for(Integer index = 0, size = result.fields.size(), index < size; index++) {
        itemData.put(result.fields[index], item[index]);
    }
    parsedObjects.add(itemData);
}

At this point, all that's left is to figure out how to parse the various attributes, which result.type_names[index] can solve inside the loop (which is why I used "Object" instead of "String" for the map value).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.