0

I have a json response from third party system which is as below.

    {
          "returnCode": 600,
          "lineItems": [
            {
              "id": 9762,
              "sfOrderId": "48393000000jK4MAAU",
              "sfLineItemId": "99303000000w9bnAAA",
              "accountType": "Indirect",
              "bounsLeads": 0,
              "cpl": 51000,
              "createdDate": "2020-07-30T14:45:16.803",
              "createdCustomerUserId": null,
              "createdPartnerUserId": 216,
              "createdInternalCustomerUserId": null,
              "createdId": null,
              "endDate": "2020-07-25T04:00:00",
              "isPrepaid": 0,
              "noIntegration": null,
              "leadVolume": 1,
              "modifiedDate": null,
              "modifiedId": null,
              "modifiedCustomerUserId": null,
              "modifiedPartnerUserId": null,
              "name": "Test file 7720202020",
              "segment": 0,
              "orderFileName": "tikto%20Test.docx",
              "orderFileLocation": "https://test.docx",
              "domainCount": null,
              "orderNumber": "",
              "poNumber": null,
              "signedDate": "2020-07-23T04:00:00",
              "startDate": "2020-07-23T04:00:00",
              "orderStatus": {
                "id": 1,
                "description": null,
                "name": "New",
                "priority": 1
              },
              "customQuestionType": null,
              "customQuestionNumber": null,
              "offerType": {
                "id": 2,
                "description": null,
                "name": "Data",
                "unitFactor": 1,
                "priority": 2
              },
              "pacingType": {
                "id": 2,
                "description": "NULL",
                "name": "Even",
                "priority": 2
              },
              "pacingInstructions": null,
              "programName": null,
              "callVerifier": null,
              "auditor": null,
              "convertorId": null,
              "formId": null,
              "linkId": null,
              "customerEmailId": null,
              "channel": null,
              "geo": "International",
              "priority": false,
              "leadCount": null,
              "contactType": null,
              "contactQuality": null,
              "suppressionListId": null,
              "namedAccountListId": null,
              "topicText": null,
              "topicsRemoved": null,
              "billingNotes": null,
              "deliveredPercentage": 0,
              "masterSuppression": null,
              "accountOwnerId": null,
              "contactOwnerId": null,
              "additionalUnits": 0,
              "contactSuppressionListId": null,
              "contactListId": null,
              "noContacts": false,
              "ioId": null,
              "advertiserId": null,
              "isSdqc": false,
              "department": null,
              "competitorTopics": false,
              "billingEmails": null
            },
            {
              "id": 9763,
              "sfOrderId": "48394000000jK4MAAU",
              "sfLineItemId": "88330000000w9boAAA",
              "accountType": "Indirect",
              "bounsLeads": 0,
              "cpl": 28000,
              "createdDate": "2020-07-30T14:45:16.805",
              "createdCustomerUserId": null,
              "createdPartnerUserId": 216,
              "createdInternalCustomerUserId": null,
              "createdId": null,
              "endDate": "2020-07-25T04:00:00",
              "isPrepaid": 0,
              "noIntegration": null,
              "leadVolume": 1,
              "modifiedDate": null,
              "modifiedId": null,
              "modifiedCustomerUserId": null,
              "modifiedPartnerUserId": null,
              "name": "Test Account 12 Test Agency 12 CPL 7720202020",
              "segment": 0,
              "orderFileName": "Testdoc.docx",
              "orderFileLocation": "https://test.docx",
              "domainCount": null,
              "orderNumber": "",
              "poNumber": null,
              "signedDate": "2020-07-23T04:00:00",
              "startDate": "2020-07-23T04:00:00",
              "orderStatus": {
                "id": 1,
                "description": null,
                "name": "New",
                "priority": 1
              },
              "customQuestionType": null,
              "customQuestionNumber": null,
              "offerType": {
                "id": 2,
                "description": null,
                "name": "Data",
                "unitFactor": 1,
                "priority": 2
              },
              "pacingType": {
                "id": 2,
                "description": "NULL",
                "name": "Even",
                "priority": 2
              },
              "pacingInstructions": null,
              "programName": null,
              "callVerifier": null,
              "auditor": null,
              "convertorId": null,
              "formId": null,
              "linkId": null,
              "customerEmailId": null,
              "channel": null,
              "geo": "International",
              "priority": false,
              "leadCount": null,
              "contactType": null,
              "contactQuality": null,
              "suppressionListId": null,
              "namedAccountListId": null,
              "topicText": null,
              "topicsRemoved": null,
              "billingNotes": null,
              "deliveredPercentage": 0,
              "masterSuppression": null,
              "accountOwnerId": null,
              "contactOwnerId": null,
              "additionalUnits": 0,
              "contactSuppressionListId": null,
              "contactListId": null,
              "noContacts": false,
              "ioId": null,
              "advertiserId": null,
              "isSdqc": false,
              "department": null,
              "competitorTopics": false,
              "billingEmails": null
            }
          ],
          "info": "Entity created."
    }
}

I am fetching the the multi line item array under the key "lineItems' by using the Json.deserializeUntyped as below

Map<String,Object> alistJson = (Map<String,Object>)((Map<String,Object>)((Map<String,Object>) JSON.deserializeUntyped(response.getbody())));
List<Object> myMaps = (List<Object>) alistJson.get('lineItems');

As there are multiple line items under that array as you can see in the above Json response, I am want to traverse each line item and pick only the sfLineItemId and Id key values and insert them into a list so that I can update them in the relevant lineItem records in Salesforce. Can anyone please let me know how this can be achieved?

3 Answers 3

1

The most simple way to do this is to use http://json2apex.herokuapp.com/. It generates you the JSON-structure that you have to Apex class(es). You can simply put your JSON String into the 'parse' function.

0

You can try this in below way. Any key value you want to pull out, use Map<String, Object> and object part you need to have typecast depending on the key part.

Map<String,Object> alistJson = (Map<String,Object>)JSON.deserializeUntyped( response.getbody() );
List<Object> myMaps = (List<object>) alistJson.get('lineItems');

for(Object myMap : myMaps){
    Map<String, Object> tempMap = (Map<String, Object>) myMap;
    System.debug('Printing JSON ::');
    for(String key: tempMap.keySet() ){
        System.debug( key + ' :: ' + tempMap.get(key));
    }
}
0

This is a two part answer:

  1. The theory: Json.Deserialize requires a type and that type can be a List. So you can run Json.deserialize(jsonTxt, List<ObjectName>.class); Your Object can (should?) be a DTO, or data transfer object. Those are inner classes that define the properties you want to deserialize by name. so something like:
 public class AccountDTO {
   String name {get;set;}
 }
  1. As Utm said above, just use http://json2apex.herokuapp.com/. as it will create your DTO's and provide unit tests etc.

You must log in to answer this question.

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