0

I'am trying to parse the 'Body' from a POST request with a Java Lambda. I'am stuck on this error for a while.

 org.json.simple.JSONObject cannot be cast to java.lang.String

But the Body when logged look like that :

{"body":{"email":"[email protected]"}}

Witch should work with the parsing i'am doing right ? The weird thing is the insert is working on local with JUNIT but not online after on AWS.

@Override
public void handleRequest(InputStream inputStream, OutputStream outputStream, Context context) throws IOException {
    logger = context.getLogger();
    BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
    Number user_id = null;
    String birthdate = null;
    List<Number> company_id = new ArrayList<Number>();
    String email = null;
    String employment_status = null;
    String firstname = null;
    String lastname = null;
    String login = null;
    String profile = null;
    List<Number> site_id = new ArrayList<Number>();
    String validation_status = null;
    JSONObject responseJson = new JSONObject();

    Map<String, AttributeValue> expressionAttributeValues = new HashMap<String, AttributeValue>();
    String filterExpression = "";
    String RegionAWS = REGION.toString();
    client = AmazonDynamoDBClientBuilder.standard().withRegion(RegionAWS).build();
    DynamoDB dynamoDB = new DynamoDB(client);
    Table table = dynamoDB.getTable("LI_user");
    try {
        JSONParser parser = new JSONParser();
        JSONObject event = (JSONObject) parser.parse(reader);
        logger.log(event.toJSONString());
        if (event.get("body") != null) {
            JSONObject bod = (JSONObject)parser.parse((String)event.get("body"));
         //   JSONObject bod = (JSONObject) event.get("body");
            if ( bod.get("id") != null) {
                user_id = (Number)bod.get("id");
            }
            if ( bod.get("birthdate") != null) {
                birthdate = (String)bod.get("birthdate");
            }
            if ( bod.get("email") != null) {
                email = (String) bod.get("email");
            }
            if ( bod.get("employment_status") != null) {
                employment_status = (String) bod.get("employment_status");
            }
            if ( bod.get("firstname") != null) {
                firstname = (String) bod.get("firstname");
            }
            if ( bod.get("lastname") != null) {
                lastname = (String) bod.get("lastname");
            }
            if ( bod.get("login") != null) {
                login = (String) bod.get("login");
            }
            if ( bod.get("profile") != null) {
                profile = (String) bod.get("profile");
            }
            if ( bod.get("validation_status") != null) {
                validation_status = (String) bod.get("validation_status");
            }
        }
1
  • You already had logger.log(event.toJSONString()); as event is a JSONObject. So why not replacing JSONObject bod = (JSONObject)parser.parse((String)event.get("body")); with JSONObject bod = (JSONObject)event.get("body"); Commented Oct 11, 2017 at 10:01

2 Answers 2

1

Replace

JSONObject bod = (JSONObject)parser.parse((String)event.get("body"));

with

JSONObject bod = (JSONObject)event.get("body");

if event is always an instance of JSONObject (as it seems granted, otherwise you would get a ClassCast at JSONObject event = (JSONObject) parser.parse(reader);)

You see logged

{"body":{"email":"[email protected]"}}

just because of logger.log(event.toJSONString());

Sign up to request clarification or add additional context in comments.

Comments

0

Yes thanks you i have progress. But now its seem like when i send the data on JSON format with Postman, that he receive a stringified version of the body. I get this data on cloudwatch logs :

     "resource": "/users",
    "httpMethod": "POST",
    "queryStringParameters": null,
    "stageVariables": null,
    "body": "{\n \"id\": 5,\n \"company_id\": [\n    1\n  ],\n \"email\": \"[email protected]\",\n \"employment_status\": \"FULL-TIME\",\n \"firstname\": \"Sebastien\",\n \"lastname\": \"LALALLALA\",\n \"login\": \"[email protected]\",\n \"profile\": \"FULL-TIME\",\n \"site_id\": \"55\",\n \"birthdate\": \"1985.05.05\",\n \"validation_status\" : \"COMPLETE\"\n}\n"
}

And this error :

java.lang.ClassCastException: java.lang.String cannot be cast to org.json.simple.JSONObject

2 Comments

Are you sending a raw json body from Postman? Did you set an contentType header? If the structure of the request is stable, you could also consider using a POJO instead than consuming from an InputStream and using so many typecasts, i.e. see docs.aws.amazon.com/lambda/latest/dg/…
Hello, yes i'am sending raw json body and content type is set to application/json

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.