3

So I am working with the D&B Hoover Direct 2.0 API to try and integrate data from there to Salesforce. I am successfully getting a JSON response that looks as follows:

{
    "FindCompanyResponse": {
        "@ServiceVersionNumber": "6.0",
        "TransactionDetail": {
            "ApplicationTransactionID": "EntyLstFindCmpny23JAN01",
            "ServiceTransactionID": "Id-bce2b7e552e5f07e183b742e-1",
            "TransactionTimestamp": "2014-01-27T00:37:08"
        },
        "TransactionResult": {
            "SeverityText": "Information",
            "ResultID": "CM000",
            "ResultText": "Success"
        },
        "FindCompanyResponseDetail": {
            "CandidateMatchedQuantity": 9753837,
            "CandidateReturnedQuantity": 2,
            "FindCandidate": [
                {
                    "DUNSNumber": "379200822",
                    "OrganizationPrimaryName": {
                        "OrganizationName": {
                            "$": "ELECTRICITAT JORDI, S.L."
                        }
                    },
                    "MarketabilityIndicator": true,
                    "PrimaryAddress": {
                        "StreetAddressLine": [
                            {
                                "LineText": "Edificio Font"
                            },
                            {
                                "LineText": "Local 2 Pb 1"
                            }
                        ],
                        "PrimaryTownName": "Andorra La Vella",
                        "CountryISOAlpha2Code": "AD",
                        "PostalCode": "AD500 AD500"
                    },
                    "TelephoneNumber": {
                        "TelecommunicationNumber": "805850"
                    },
                    "FacsimileNumber": {
                        "TelecommunicationNumber": "805854"
                    },
                    "StandaloneOrganizationIndicator": true,
                    "IndustryCode": [
                        {
                            "@DNBCodeValue": 3599,
                            "@TypeText": "D&B Standard Industry Code",
                            "IndustryCode": {
                                "$": "17310000"
                            },
                            "IndustryCodeDescription": [
                                {
                                    "@IndustryCodeDescriptionLengthCode": 2121,
                                    "$": "ELECTRICAL WORK"
                                }
                            ]
                        }
                    ],
                    "ConsolidatedEmployeeDetails": {
                        "TotalEmployeeQuantity": 6
                    },
                    "PubliclyTradedCompanyIndicator": false,
                    "UpwardFamilyMemberPubliclyTradedCompanyIndicator": false,
                    "SalesRevenueAmount": {
                        "@CurrencyISOAlpha3Code": "USD",
                        "@UnitOfSize": "Million",
                        "$": 0.95148
                    },
                    "DisplaySequence": 1
                },
                {
                    "DUNSNumber": "464375850",
                    "OrganizationPrimaryName": {
                        "OrganizationName": {
                            "$": "ALEXANDRE SELECCIONS GASTRONOMIQUES SL"
                        }
                    },
                    "TradeStyleName": [
                        {
                            "OrganizationName": {
                                "$": "ASG"
                            }
                        }
                    ],
                    "MarketabilityIndicator": true,
                    "PrimaryAddress": {
                        "StreetAddressLine": [
                            {
                                "LineText": "Ctra De La Comella 130 Edf Forn Elies"
                            }
                        ],
                        "PrimaryTownName": "Andorra La Vella",
                        "CountryISOAlpha2Code": "AD",
                        "PostalCode": "AD500 AD500"
                    },
                    "TelephoneNumber": {
                        "TelecommunicationNumber": "864242"
                    },
                    "FacsimileNumber": {
                        "TelecommunicationNumber": "863438"
                    },
                    "StandaloneOrganizationIndicator": true,
                    "IndustryCode": [
                        {
                            "@DNBCodeValue": 3599,
                            "@TypeText": "D&B Standard Industry Code",
                            "IndustryCode": {
                                "$": "51490000"
                            },
                            "IndustryCodeDescription": [
                                {
                                    "@IndustryCodeDescriptionLengthCode": 2121,
                                    "$": "GROCERIES,REL PDS NEC"
                                }
                            ]
                        }
                    ],
                    "ConsolidatedEmployeeDetails": {
                        "TotalEmployeeQuantity": 5
                    },
                    "PubliclyTradedCompanyIndicator": false,
                    "UpwardFamilyMemberPubliclyTradedCompanyIndicator": false,
                    "SalesRevenueAmount": {
                        "@CurrencyISOAlpha3Code": "USD",
                        "@UnitOfSize": "Million",
                        "$": 0.92505
                    },
                    "DisplaySequence": 2
                }
            ]
        }
    }
}

The problem I am running into is that some of the data I need (org Name, address, revenue etc) have special characters like $ and @ that are reserved in Apex, preventing the de-serialization from working properly when I create a class using https://json2apex.herokuapp.com/ . I cannot create data structures to store these values as Apex will not allow $ and @ in variable names and class names.

If anyone has any ideas or suggestions to create an Apex class to properly handle the deserialization that would be greatly appreciated. I am beginning to think I may have to use the JSON parser method to manually parse the response and create classes/data members accordingly.

1 Answer 1

7

From a few minutes of looking, the best option looks like using the JSON2Apex "Create explicit parse code" option as a starting point because that generates code such as this:

if (text == '@CurrencyISOAlpha3Code') {
    @CurrencyISOAlpha3Code = parser.getText();

and it would be pretty simple to knock off the awkward characters from only the variable names:

if (text == '@CurrencyISOAlpha3Code') {
    CurrencyISOAlpha3Code = parser.getText();

Alternatively, for this sort of problem, JSON.deserializeUntyped where the data is pulled into a tree of Map<String, Object> so arbitrary strings can be kept in the keys can be a good way to go, particularly if you only want to extract a few values by pulling those values out using the key value strings.

3
  • Thanks for the tips, I will try that out now and see if it works! Jul 18, 2018 at 14:43
  • For what it's worth, JSON2Apex hasn't been updated to use a switch, either, which should have considerable performance benefits over the chained-if-else method.
    – sfdcfox
    Jul 18, 2018 at 15:09
  • @sfdcfox Yeah it's a good example of where switch might help (for large data volumes). Another JSON2Apex tweak would be to automatically sanitise the field names...
    – Keith C
    Jul 18, 2018 at 16:15

You must log in to answer this question.

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