0

We used to deserialize the JSON when we know the object type . but this JSON is dynamic and I have to get the object name,fieldname and its values and store it in any collection where I can reuse it later.

{
    "Header": {
        "Assessment": "A-1035",
        "Type": "Assessment",
        "SFID": "a3C4C0000000ZHZUA2",
        "FarmName": "Universal Farms",
        "UserName": "Roy Human",
        "UserEmail": "[email protected]",
        "UserID": "00544000007wwMAAAY"
    },
    "Attributes": {
        "Bedding": 0,
        "Stalls": 0
    },
    "Data": [
        ["COWS_Assessment__c.ID",
            "a3C4C0000000ZHZUA2"
        ],
        ["COWS_Assessment__c.Name",
            "A-1035"
        ],
        [
            "COWS_Assessment__c.A_Notes__c",
            "a test that includes \"quotes\" in a text box."
        ],
        [
            "COWS_Assessment__c.A_Benchmark_Type__c",
            "null"
        ],
        [
            "COWS_Assessment__c.A_Assessment_Date__c",
            ""
        ],
        [
            "COWS_Assessment__c.A_Interviewee_Name__c",
            "null"
        ],
        [
            "COWS_Assessment__c.A_Interviewee_Title__c",
            "null"
        ],
        [
            "COWS_Assessment__c.A_Interviewer__c",
            "null"
        ],
        [
            "COWS_Assessment__c.A_PenNumName__c",
            "null"
        ],
        [
            "COWS_Assessment__c.A_Assessment_Type__c",
            "null"
        ],
        [
            "COWS_Assessment__c.A_Valid_Benchmark__c",
            "null"
        ],
        [
            "COWS_Assessment__c.CIP_Bulls_AggrCows__c",
            "null"
        ],
        [
            "COWS_Assessment__c.CIP_FirstLact__c",
            "null"
        ],
        [
            "COWS_Assessment__c.CIP_MilkingTimes__c",
            "null"
        ],
        [
            "COWS_Assessment__c.CIP_MilkingTimesperDay__c",
            "null"
        ],
        [
            "COWS_Assessment__c.CIP_NumCowsInPen__c",
            "null"
        ],
        [
            "COWS_Assessment__c.CIP_PenAveFat__c",
            "null"
        ],
        [
            "COWS_Assessment__c.CIP_PenAveLact__c",
            "null"
        ],
        [
            "COWS_Assessment__c.CIP_PenAveMilk__c",
            "null"
        ],
        [
            "COWS_Assessment__c.CIP_PenAveProtein__c",
            "null"
        ],
        [
            "COWS_Assessment__c.CIP_PenAveSCC__c",
            "null"
        ],
        [
            "COWS_Assessment__c.CIP_PenDescription__c",
            "null"
        ],
        [
            "COWS_Assessment__c.Hock_Score_1__c",
            "null"
        ],
        [
            "COWS_Assessment__c.Hock_Score_2__c",
            "null"
        ],
        [
            "COWS_Assessment__c.Hock_Score_3__c",
            "null"
        ],
        [
            "COWS_Assessment__c.MC_BreedOfMajority__c",
            "null"
        ],
        [
            "COWS_Assessment__c.MC_FarmAveDIM__c",
            "null"
        ],
        [
            "COWS_Assessment__c.MC_FarmAveLact__c",
            "null"
        ],
        [
            "COWS_Assessment__c.MC_FarmAveMilk__c",
            "null"
        ],
        [
            "COWS_Assessment__c.MC_NumCowsAssessed__c",
            "null"
        ],
        [
            "COWS_Assessment__c.MC_NumCowsMilked__c",
            "null"
        ],
        [
            "COWS_Assessment__c.MC_TotalCowsInHerd__c",
            "null"
        ],
        [
            "COWS_Assessment__c.Parlor_BarnType__c",
            "null"
        ],
        [
            "COWS_Assessment__c.Parlor_FeedingFreq__c",
            "null"
        ],
        [
            "COWS_Assessment__c.Parlor_FeedingTimes__c",
            "null"
        ],
        [
            "COWS_Assessment__c.Parlor_NumUnits__c",
            "null"
        ],
        [
            "COWS_Assessment__c.Parlor_PushUpFreq__c",
            "null"
        ],
        [
            "COWS_Assessment__c.Parlor_Type__c",
            "null"
        ],
        [
            "COWS_Assessment__c.Parlor_YearBarnBuilt__c",
            "null"
        ],
        [
            "COWS_Assessment__c.PE_HoldingAreaCooling__c",
            "null"
        ],
        [
            "COWS_Assessment__c.PE_PenCooling__c",
            "null"
        ],
        [
            "COWS_Assessment__c.PE_HoldingFanTemp__c",
            "null"
        ],
        [
            "COWS_Assessment__c.PE_HoldingSprinklerTemp__c",
            "null"
        ],
        [
            "COWS_Assessment__c.PE_PenFanTemp__c",
            "null"
        ],
        [
            "COWS_Assessment__c.PE_PenSprinklerTemp__c",
            "null"
        ],
        ["COWS_Farm__c.Name",
            "Kambro Farms"
        ],
        [
            "COWS_Farm__c.Contact_Person__c",
            "Tom Kampman"
        ],
        [
            "COWS_Farm__c.Contact_Role__c",
            "General Manager"
        ],
        [
            "COWS_Farm__c.Farm_Address__c",
            "33344 Township Rd."
        ],
        [
            "COWS_Farm__c.Farm_City__c",
            "Abbotsford"
        ],
        [
            "COWS_Farm__c.Farm_State__c",
            "BC"
        ],
        [
            "COWS_Farm__c.Farm_Zip_Code__c",
            "null"
        ],
        [
            "COWS_Farm__c.Farm_Phone__c",
            "(604) 768-9093"
        ],
        [
            "COWS_Farm__c.Farm_Email__c",
            "[email protected]"
        ],
        [
            "COWS_Farm__c.Producer_Name__c",
            "Tom Kampman"
        ],
        [
            "COWS_Farm__c.Farm_Type__c",
            "Freestall"
        ],
        [
            "COWS_Farm__c.Consultant_Name__c",
            "Steve"
        ],
        [
            "COWS_Farm__c.Consultant_Phone_Number__c",
            "(604) 217-6757"
        ],
        [
            "COWS_Farm__c.Consultant_Email__c",
            "[email protected]"
        ],
        [
            "COWS_Farm__c.Farm_Mailing_Address__c",
            "null"
        ],
        [
            "COWS_Farm__c.Farm_Mailing_City__c",
            "null"
        ],
        [
            "COWS_Farm__c.Farm_Mailing_State__c",
            "null"
        ],
        [
            "COWS_Farm__c.Farm_Mailing_Zip_Code__c",
            "null"
        ],
        ["COWS_Farm__c.Notes__c",
            "Products used: Alimet, Mintrex, Santoquin.\nEnter the farm through the south gate."
        ],
        [
            "COWS_Farm__c.Number_of_Employees__c",
            "32"
        ],
        [
            "COWS_Farm__c.Products_Used__c",
            "Antioxidants;Minerals;Vitamins;Essential Oils"
        ],
        [
            "COWS_Farm__c.Veterinarian__c",
            "AgWest Veterinarian Group"
        ],
        [
            "COWS_Farm__c.Novus_Sales_Manager__c",
            "Clemence Nash"
        ],
        [
            "COWS_Farm__c.Company_Name__c",
            "AAA Egg"
        ],
        [
            "COWS_Farm__c.Business_Opportunity__c",
            "Prospect"
        ]
    ]
}
2
  • Maybe take a look at json2apex.herokuapp.com ?
    – Girbot
    Oct 19, 2018 at 16:32
  • 1
    If you have a dynamic JSON everytime, then that's an issue in itself. Because if you are expecting a JSON, it should follow a schema. And with whatever wrapper class you write, you need to make sure that all those fields which you expect to deserialize are present in the JSON. When you say dynamic - what exactly changes every time?
    – Jayant Das
    Oct 19, 2018 at 16:38

1 Answer 1

2

I'm going to go ahead and assume that the structure of the JSON is relatively constant, regardless of the object/fields? Following this general syntax:

{
  Header:{...},
  Attributes:{...},
  Data:[
    ['Field','Value'],  // Are you sure this isn't {'Field':'Value'} ?
  ],
}

This is where generic sObjects come in handy.

You can use sObject.put(fieldName,value) to assign field values to the generic sObject type by their name.

When initializing the sObject, you can use Schema.getGlobalDescribe().get(objectType).newSObject() to create an instance of it from a String of the object type.

Then, once you convert the JSON into a Salesforce equivalent to iterate over, you can simply init a new sObject, and then iterate over your data to dynamically assign the field values, assuming they map to a Salesforce Object!

Here is a very basic example showing loading data into the account object this way:

    String json_string = '{"Header":{"type":"Account"},"Data":[["Name","Hello World!"]]}';

    // Break the JSON String into a Map
    Map<String,Object> json_object = (Map<String, Object>)JSON.deserializeUntyped(json_string);      

    // It further breaks down into another Map and a List      
    List<Object> Data = (List<Object>)json_object.get('Data');
    Map<String,Object> Header = (Map<String,Object>)json_object.get('Header');

    // Get object type and initialize an sObject
    String obj_type = (String)Header.get('type');        
    sObject obj = Schema.getGlobalDescribe().get(obj_type).newSObject();

    // Iterate through the data and dynamically assign fields
    for (Object d : Data) {
        List<Object> field_value = (List<Object>)d;
        obj.put((String)field_value[0],(String)field_value[1]);
    }

    insert obj;

Hope this helps.

1
  • 1
    here is an answer I prepared on Dynamic Json that i use to answer questions like this. I'm adding it as a comment here since your answer is the best answer submitted. salesforce.stackexchange.com/a/227588/24107
    – gNerb
    Oct 19, 2018 at 18:15

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .