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");
}
}
logger.log(event.toJSONString());aseventis aJSONObject. So why not replacingJSONObject bod = (JSONObject)parser.parse((String)event.get("body"));withJSONObject bod = (JSONObject)event.get("body");