0

I'm running this code in the execute anonymous console and it works, the record gets assigned to the correct asset as expected..

    String data = '{"DOKA_Device_Id__c": "DOKA789123456","Opening_Speed__c": 50.5,"Force_Limit_Open__c": 150}';

Door_Event__c newEvent = new Door_Event__c();

JSONParser parser = JSON.createParser(data);

String fieldName;
String fieldValTxt;
Decimal fieldValNum;

parser.nextToken();

//advance to the next token
while (parser.nextToken() != JSONToken.END_OBJECT) {

    //if a name token, populate the fieldName 
    IF (parser.getCurrentToken() == JSONToken.FIELD_NAME) {

        fieldName = parser.getCurrentName();
        parser.nextToken();
    }

    // add strings to text values and numbers to number values    
    IF (parser.getCurrentToken() == JSONToken.VALUE_STRING){

        fieldValTxt = parser.getText();
        System.debug('first one should be DOKA: ' + fieldValTxt);

        IF (fieldName == 'DOKA_Device_Id__c') {
            Asset newDoor = [SELECT Id from Asset where Doka_Device_Id__c = :fieldValTxt LIMIT 1];
            newEvent.Asset__c = newDoor.Id;
            System.debug('Asset is: ' + newEvent.Asset__c);

        }else{
        newEvent.put(fieldName, fieldValTxt);

        }    
    }else if (parser.getCurrentToken() == JSONToken.VALUE_NUMBER_INT){

        fieldValNum = parser.getDecimalValue();
        newEvent.put(fieldName, fieldValNum);

    }else if (parser.getCurrentToken() == JSONToken.VALUE_NUMBER_FLOAT){

        fieldValNum = parser.getDecimalValue();
        newEvent.put(fieldName, fieldValNum);

    }              
}


insert newEvent;

However when i submit this through an authenticated postman session (it's a @HttpPost class), i'm getting the following error..

   [
      {
        "message": "Unexpected parameter encountered during deserialization: DOKA_Device_Id__c at [line:2, column:24]",
        "errorCode": "JSON_PARSER_ERROR"
      }
    ]

The data being submitted is a copy/paste from the String value in the anon console, but I can't figure out why this JSON Parser error is being thrown.

The HttpPost is below:

    @HttpPost
global static Door_Event__c sendDoor(String data) {

  //felipe
  System.debug('The data is: ' + data);

  Door_Event__c newEvent = new Door_Event__c();

  JSONParser parser = JSON.createParser(data);

  String fieldName;
  String fieldValTxt;
  Decimal fieldValNum;

  parser.nextToken();

  //advance to the next token
  while (parser.nextToken() != JSONToken.END_OBJECT) {

    //if a name token, populate the fieldName
      IF (parser.getCurrentToken() == JSONToken.FIELD_NAME) {

          fieldName = parser.getCurrentName();
          parser.nextToken();
      }

    // add strings to text values and numbers to number values
      IF (parser.getCurrentToken() == JSONToken.VALUE_STRING){

          fieldValTxt = parser.getText();
          System.debug('first one should be DOKA: ' + fieldValTxt);

          IF (fieldName == 'DOKA_Device_Id__c') {
              Asset newDoor = [SELECT Id from Asset where Doka_Device_Id__c = :fieldValTxt LIMIT 1];
              newEvent.Asset__c = newDoor.Id;
              System.debug('Asset is: ' + newEvent.Asset__c);

          }else{
          newEvent.put(fieldName, fieldValTxt);

          }
      }else if (parser.getCurrentToken() == JSONToken.VALUE_NUMBER_INT){

          fieldValNum = parser.getDecimalValue();
          newEvent.put(fieldName, fieldValNum);

      }else if (parser.getCurrentToken() == JSONToken.VALUE_NUMBER_FLOAT){

          fieldValNum = parser.getDecimalValue();
          newEvent.put(fieldName, fieldValNum);

      }
  }


  insert newEvent;
  return newEvent;
}

2 Answers 2

2

As per Salesforce documentation, the correct JSON data structure for your case should be.

{"data": {"DOKA_Device_Id__c": "DOKA789123456","Opening_Speed__c": 50.5,"Force_Limit_Open__c": 150}}

Update

I completely missed your post method signature. The post method signature should be

global static Door_Event__c sendDoor() {

And retrive the JSON string by using RestContext and parse it by JSON Parser.

String data = RestContext.request.requestBody.toString();
JSONParser parser = JSON.createParser(data);

I hope it will work now.

2
  • Thanks Saroj, but it's still the same error with that format passed into it as well.. Nov 15, 2016 at 11:21
  • @adam.sellers please check my updated answer.
    – Saroj Bera
    Nov 15, 2016 at 12:45
3

Related to Saroj's answer, the way you have coded your @HttpPost method does introduce an extra object level into the JSON that you probably don't want. You can/should avoid that by using this alternative pattern where you take the data from the request yourself:

private class DoorEvent {
    public String DOKA_Device_Id__c;
    public Decimal Opening_Speed__c;
    public Integer Force_Limit_Open__c;
}

@HttpPost
global static Door_Event__c sendDoor() {

    String data = RestContext.request.requestBody.toString();
    DoorEvent de = (DoorEvent) JSON.deserialize(data, DoorEvent.class);
    ...

This also illustrates the use of the JSON class to parse into a class generated (and then modified a bit) by https://json2apex.herokuapp.com/.

You must log in to answer this question.

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