I have been several posts recently on the Developer Boards around JSON deserialization and some weird and convoluted ways to convert it into something that is useful for Salesforce. Let’s talk about what I have found is the cleanest way to handle JSON deserialization.
JSON Payload
Let’s take a look at our JSON payload. I am taking the payload from the docsample Heroku app since it’s an easy way to get consistent data from a webservice.
{
"invoiceList": [
{
"totalPrice": 5.5,
"statementDate": "2011-10-04T16:58:54.858Z",
"lineItems": [
{
"UnitPrice": 1,
"Quantity": 5,
"ProductName": "Pencil"
},
{
"UnitPrice": 0.5,
"Quantity": 1,
"ProductName": "Eraser"
}
],
"invoiceNumber": 1
},
{
"totalPrice": 11.5,
"statementDate": "2011-10-04T16:58:54.858Z",
"lineItems": [
{
"UnitPrice": 6,
"Quantity": 1,
"ProductName": "Notebook"
},
{
"UnitPrice": 2.5,
"Quantity": 1,
"ProductName": "Ruler"
},
{
"UnitPrice": 1.5,
"Quantity": 2,
"ProductName": "Pen"
}
],
"invoiceNumber": 2
}
]
}
So we can see here that the data provided is an invoice list and each invoice contains data and line items for that invoice.