0

This is my json object when i invoke API

job

{
    "path": "/",
    "isBase64Encoded": false,
    "requestContext": {
        "resourceId": "20gm43r4fa",
        "resourcePath": "/",
        "httpMethod": "POST",
        "extendedRequestId": "GO6ktGL_IAMFaLA=",
        "requestTime": "13/Jan/2020:09:53:34 +0000",
        "path": "/",
        "accountId": "***",
        "protocol": "HTTP/1.1",
        "stage": "test-invoke-stage",
        "domainPrefix": "testPrefix",
        "requestTimeEpoch": 1578909214062,
        "requestId": "dgdfg-ad12-431a-b1b9-***",
        "identity": {
            "accountId": "***",
            "caller": "345345:a.b@***.com",
            "apiKey": "test-invoke-api-key",
            "sourceIp": "test-invoke-source-ip",
            "accessKey": "&&YY&&YH",
            "userArn": "arn:aws:sts::***:assumed-role/****-PowerUser/ab.dx@****.com",
            "apiKeyId": "test-invoke-api-key-id",
            "userAgent": "aws-internal/3 aws-sdk-java/1.11.690 Linux/4.9.184-0.1.ac.235.83.329.metal1.x86_64 OpenJDK_64-Bit_Server_VM/25.232-b09 java/1.8.0_232 vendor/Oracle_Corporation",
            "user": "345345345:Sa.b@***.com"
        },
        "domainName": "testPrefix.testDomainName",
        "apiId": "dfgdf"
    },
    "resource": "/",
    "httpMethod": "POST",
    "body": "{\"ID\":\"sup-9749-0e710000fd04\",\"VERSION\":1,\"AUDIT_EVENT_TO_DATE_TP\":null}"
}

Body of the JSON is something like this

bodyofJson

{
    "ID": "sup-9749-0e710000fd04",
    "VERSION": 1,
    "ACTION_TYPE": "NEW_CASE",
    "EVENT_TYPE": "WORLDCHECK"
}

I need to get the body of the json and key as ID which is inside Body json . I am able to get Body but not able to get ID from body json

This is what i am doing

JSONObject job = new JSONObject(json);
System.out.println("****job*******"+job);
String bodyofJson = job.getString("body");
System.out.println("****bodyofJson*******"+bodyofJson);

The position of the ID can be changing also so thats why i am not getting based on index

Please suggest

4
  • 2
    I'm not sure if I understand correctly, but if body itself contains JSON, can't you just parse it as you did with json and get the ID field? Commented Jan 13, 2020 at 10:09
  • @FedericoklezCulloca i tried that but it gives me error Commented Jan 13, 2020 at 10:19
  • Please edit your question to include the code you tried and the error it gives. Commented Jan 13, 2020 at 10:20
  • I see that in the job example you gave, body has value "{\"ID\":\"sup-9749-0e710000fd04\",\"VERSION\":1,\"AUDIT_EVENT_TO_DATE_TP\":null}" which you won't be able to parse as json because it's not valid (null is not a valid value. "null", notice the quotation marks, is). Commented Jan 13, 2020 at 10:22

1 Answer 1

1

body key further contains a json. So, we need to parse it as JSONObject and get the value of ID.

Here is the code:

JSONObject job = new JSONObject(json);
System.out.println("****job*******"+job);
String bodyofJson = job.getString("body");
System.out.println("****bodyofJson*******"+bodyofJson);
// Parse bodyofJson as JSONObject
JSONObject bodyJsonObj = new JSONObject(bodyofJson);
System.out.println(bodyJsonObj.get("ID"));
Sign up to request clarification or add additional context in comments.

Comments

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.