2

On workbench I am getting:

message: Unexpected parameter encountered during deserialization: response at [line:2, column:18] errorCode: JSON_PARSER_ERROR

I validated the JSON so I don't know where the problem is. Any help is appreciated.

Apex Class

@RestResource(urlMapping='/JSON2APEX/*')
global without sharing class JSON2Apex {

    public class Status {
        public String type;
        public String message;
        public Integer code;
        public String code_message;
    }

    public class Requests {
        public Request request;
    }

    public Response response;

    public class Response {
        public Integer count;
        public Double benchmark;
        public List<Requests> requests;
        public Status status;
    }

    public class Request {
        public Integer id;
        public String image_thumbnail;
        public String title;
        public String description;
        public String status;
        public String address;
        public String location;
        public String zipcode;
        public String user;
        public Integer date_created;
        public Integer count_comments;
        public Integer count_followers;
        public Integer count_supporters;
        public Double lat;
        public Double lon;
        public Integer user_follows;
        public Integer user_comments;
        public Integer user_request;
        public String rank;
    }

    @httppost
    global static JSON2Apex parse(String json) {
        return (JSON2Apex) System.JSON.deserialize(json, JSON2Apex.class);
    }
}

Payload:

{
"response": {
    "count": 1,
    "benchmark": 0.22567009925842,
    "requests": [
        {
            "request": {
                "id": 537481,
                "image_thumbnail": "",
                "title": "Request for new bin(s) - residential",
                "description": "Propmain ref  3234-1114",
                "status": "submitted",
                "address": "36 Pine Tree Close",
                "location": "Peterborough, England",
                "zipcode": "PE1 1EJ",
                "user": "",
                "date_created": 1417173208,
                "count_comments": 0,
                "count_followers": 0,
                "count_supporters": 0,
                "lat": 52.599967,
                "lon": -0.233482,
                "user_follows": 0,
                "user_comments": 0,
                "user_request": 1,
                "rank": "0"
            }
        }
    ],
    "status": {
        "type": "success",
        "message": "Success",
        "code": 200,
        "code_message": "Ok"
    }
}
}

1 Answer 1

2

The issue is because of this line:

global static JSON2Apex parse(String json) {

SFDC will attempt to automatically deserialize the POST payload as a JSON string into an SFDC string. But your POST payload is a JSON object, not a JSON string.

To resolve, you can do the following:

@httppost
global static JSON2Apex parse() {
    String json = RestContext.request.requestBody.toString();
    return (JSON2Apex) System.JSON.deserialize(json, JSON2Apex.class);
}

Reference from Apex REST Doc

If the Apex method has no parameters, Apex REST copies the HTTP request body into the RestRequest.requestBody property. If the method has parameters, then Apex REST attempts to deserialize the data into those parameters and the data won't be deserialized into the RestRequest.requestBody property.

2
  • thank you! I didn't know this. Do you know where this was documented? Commented Dec 25, 2019 at 7:30
  • I added the reference above.
    – cropredy
    Commented Dec 25, 2019 at 21:22

You must log in to answer this question.

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