Accessing values within an array

I have a request that gets the following body in the response:

[
{
“Request”: {
“id”: 281,
“snaplogicrequestid”: “onemd4cjobidojsubwhoym”,
“overallstatus”: “finished incomplete”,
“statussent”: true,
“completed”: true,
“updatedfrom”: “/SIE_DI_TEST/ACAP/Generator_Results/03_Generate_Task_Results”,
“jobid”: “onemd4cjobidojsubwhoym”,
“payload”: “{"metadata":{"topic":"AssetCollector_Task_In","partition":3,"offset":96,"consumer_group":"acap_qa_cg1","client_id":"1686552294401|ip-10-138-159-70.eu-central-1.compute.internal|/SIE_DI_TEST/ACAP/OneMD4C_Tasks/01_OneMD4C_Tasks/OneMD Tasks Consumer|6007d3c7c5702041dcf5166c_a477d5a1-dc27-4b52-8323-245c3a72e008|d1f8face-cc19-4d74-b2a4-fee8e2d73fa2","tracker_index":0,"batch_size":1,"batch_index":0,"record_index":0,"auto_commit":false},"timeNow":"2023-06-12T08:47:08.379Z","snaplogicrequestid":"onemd4cjobidojsubwhoym","Task":{"articleNumberlistRef":"sftp://acapns.upload.akamai.com/1389923/articlenumberlist-test1.csv","configurationName":"Catalogue Download Oct 22","customerID":"cust123456","jobID":"onemd4cjobidojsubwhoym","language":"en-EN","region":"DE","request":[{"pictures":{"productphoto":{"format":[1,9,16,19,49,59,63,64,68],"url":"false"},"dimensiondrawing":{"format":[9,5,19,16,59,45,63,44],"url":"true"},"model3D":{"format":[9,51,19,16,73,11],"url":"true"},"caeeplanMakro":{"format":[69],"url":"true"},"caeConnDiagram":{"format":[51],"url":"true"},"unitwiringdiagramIEC":{"format":[19,5,16,9,59,45,44],"url":"true"},"unitwiringdiagramANSI":{"format":[19,5,16,9,59,44],"url":"true"},"diagram2D":{"format":[19,5,16,9,59,44],"url":"true"}}}]}}”,
“resultpayload”: “{"jobId":"onemd4cjobidojsubwhoym","status":"finished incomplete","statuscode":41,"statusmessage":"Package available, with the available assets (less than requested amount)","progress":100,"ResultURL":"sftp://acapns.upload.akamai.com/1389923/provisioning_oneMD4C/onemd4cjobidojsubwhoym.zip","LogURL":"sftp://acapns.upload.akamai.com/1389923/provisioning_oneMD4C/onemd4cjobidojsubwhoym.zip","Request":[{"pictures":{"status":"finished incomplete","statuscode":41,"statusmessage":"63 products exported out of 308","errorcode":""}}]}”,
“articlenumberlistref”: “sftp://acapns.upload.akamai.com/1389923/articlenumberlist-test1.csv”,
“resulturl”: “sftp://acapns.upload.akamai.com/1389923/provisioning_oneMD4C/onemd4cjobidojsubwhoym.zip”,
“customerid”: “cust123456”,
“language”: “en-EN”,
“generators”: 1,
“lastmodified”: “2023-06-12T08:50:37.190”,
“requestreceivedtimestamp”: “2023-06-12T08:47:08.379”,
“requestcompletetimestamp”: “2023-06-12T08:50:37.190”,
“statussenttimestamp”: “2023-06-12T08:50:37.190”,
“sendattemptcount”: 0
}
},
{
“Generator”: {
“id”: 323,
“snaplogicrequestid”: “onemd4cjobidojsubwhoym”,
“generator”: “BILDDB”,
“updatedfrom”: “/SIE_DI_TEST/ACAP/Generator_Results/02_Process_Record”,
“totalproducts”: 308,
“status”: “finished incomplete”,
“statuscode”: 41,
“statusmessage”: “pictures have been partially exported”,
“errorcode”: “”,
“exportedproducts”: 63,
“resulturl”: “sftp://acapns.upload.akamai.com/1389923/delivery_Pictures/onemd4cjobidojsubwhoym.zip”,
“logurl”: “sftp://acapns.upload.akamai.com/1389923/delivery_Pictures/onemd4cjobidojsubwhoym.zip”,
“lastmodified”: “2023-06-12T08:50:23.044”,
“progress”: 100
}
},
{
“GeneratorLog”: {
“snaplogicrequestid”: “onemd4cjobidojsubwhoym”,
“logtype”: “BIDDB Generator Status Update”,
“generator”: “BILDDB”,
“updatedfrom”: “/SIE_DI_TEST/ACAP/BILDDB_Subtasks/02_Process_Message”,
“lastmodified”: “2023-06-12T08:49:35.242”,
“success”: true,
“responsestatus”: null,
“responseidentifier”: “”,
“message”: “request sent to generator”,
“requestpayload”: “{"jobId":"onemd4cjobidojsubwhoym#BILDDB","status":"received","exportedproducts":0,"statuscode":20,"statusmessage":"request sent to generator","errorcode":"","progress":20,"ResultURL":"","LogURL":""}”,
“responsepayload”: null,
“argsmetadata”: “{"SnaplogicRequestId":"onemd4cjobidojsubwhoym","MLFBFilePath":"sftp://acapns.upload.akamai.com/1389923/articlenumberlist-test1.csv"}”
}
},
{
“GeneratorLog”: {
“snaplogicrequestid”: “onemd4cjobidojsubwhoym”,
“logtype”: “BIDDB Generator Status Update”,
“generator”: “BILDDB”,
“updatedfrom”: “/SIE_DI_TEST/ACAP/BILDDB_Subtasks/02_Process_Message”,
“lastmodified”: “2023-06-12T08:49:35.243”,
“success”: true,
“responsestatus”: null,
“responseidentifier”: “”,
“message”: “job accepted”,
“requestpayload”: “{"jobId":"onemd4cjobidojsubwhoym#BILDDB","status":"in progress","exportedproducts":0,"statuscode":30,"statusmessage":"job accepted","errorcode":"","progress":30,"ResultURL":"","LogURL":""}”,
“responsepayload”: null,
“argsmetadata”: “{"SnaplogicRequestId":"onemd4cjobidojsubwhoym","MLFBFilePath":"sftp://acapns.upload.akamai.com/1389923/articlenumberlist-test1.csv"}”
}
},
{
“GeneratorLog”: {
“snaplogicrequestid”: “onemd4cjobidojsubwhoym”,
“logtype”: “BIDDB Generator Status Update”,
“generator”: “BILDDB”,
“updatedfrom”: “/SIE_DI_TEST/ACAP/BILDDB_Subtasks/02_Process_Message”,
“lastmodified”: “2023-06-12T08:49:35.243”,
“success”: true,
“responsestatus”: null,
“responseidentifier”: “”,
“message”: “job in work”,
“requestpayload”: “{"jobId":"onemd4cjobidojsubwhoym#BILDDB","status":"in progress","exportedproducts":0,"statuscode":30,"statusmessage":"job in work","errorcode":"","progress":30,"ResultURL":"","LogURL":""}”,
“responsepayload”: null,
“argsmetadata”: “{"SnaplogicRequestId":"onemd4cjobidojsubwhoym","MLFBFilePath":"sftp://acapns.upload.akamai.com/1389923/articlenumberlist-test1.csv"}”
}
},
{
“GeneratorLog”: {
“snaplogicrequestid”: “onemd4cjobidojsubwhoym”,
“logtype”: “BIDDB Generator Status Update”,
“generator”: “BILDDB”,
“updatedfrom”: “/SIE_DI_TEST/ACAP/BILDDB_Subtasks/02_Process_Message”,
“lastmodified”: “2023-06-12T08:49:35.243”,
“success”: true,
“responsestatus”: null,
“responseidentifier”: “”,
“message”: “pictures have been partially exported”,
“requestpayload”: “{"jobId":"onemd4cjobidojsubwhoym#BILDDB","status":"finished incomplete","exportedproducts":63,"statuscode":41,"statusmessage":"pictures have been partially exported","errorcode":"","progress":100,"ResultURL":"sftp://acapns.upload.akamai.com/1389923/delivery_Pictures/onemd4cjobidojsubwhoym.zip","LogURL":"sftp://acapns.upload.akamai.com/1389923/delivery_Pictures/onemd4cjobidojsubwhoym.zip"}”,
“responsepayload”: null,
“argsmetadata”: “{"SnaplogicRequestId":"onemd4cjobidojsubwhoym","MLFBFilePath":"sftp://acapns.upload.akamai.com/1389923/articlenumberlist-test1.csv"}”
}
}
]

My goal is to log on the console the different values within the bold text, specifically the italic “status code” value.

I can access the entire resultpayload with:

var jsonData = pm.response.json();
console.log(jsonData[0].Request.resultpayload);

But I am having trouble getting the properties inside it.

I tried console.log(jsonData[0].Request.resultpayload[2]);
But that gives me “j” (the 3rd character) instead of the statuscode property I’m looking for.

I also tried console.log(jsonData[0].Request.resultpayload.statuscode);
But that gives undefined error.

Thank you in advance for any help.

Can you repost your JSON sample using the preformatted text option in the editor.

This will stop everything from being aligned to the left at which point we can help with the targeting of the elements.

[
    {
        "Request": {
            "id": 281,
            "snaplogicrequestid": "onemd4cjobidojsubwhoym",
            "overallstatus": "finished incomplete",
            "statussent": true,
            "completed": true,
            "updatedfrom": "/SIE_DI_TEST/ACAP/Generator_Results/03_Generate_Task_Results",
            "runtimeurl": "https://cdn.elastic.snaplogic.com/sl/designer.html#pipe_runtime_id=6007d3c7c5702041dcf5166c_d56e39ef-80af-4a9e-8bc9-7390efdb0b95",
            "jobid": "onemd4cjobidojsubwhoym",
            "payload": "{\"metadata\":{\"topic\":\"AssetCollector_Task_In\",\"partition\":3,\"offset\":96,\"consumer_group\":\"acap_qa_cg1\",\"client_id\":\"1686552294401|ip-10-138-159-70.eu-central-1.compute.internal|/SIE_DI_TEST/ACAP/OneMD4C_Tasks/01_OneMD4C_Tasks/OneMD Tasks Consumer|6007d3c7c5702041dcf5166c_a477d5a1-dc27-4b52-8323-245c3a72e008|d1f8face-cc19-4d74-b2a4-fee8e2d73fa2\",\"tracker_index\":0,\"batch_size\":1,\"batch_index\":0,\"record_index\":0,\"auto_commit\":false},\"timeNow\":\"2023-06-12T08:47:08.379Z\",\"snaplogicrequestid\":\"onemd4cjobidojsubwhoym\",\"Task\":{\"articleNumberlistRef\":\"sftp://acapns.upload.akamai.com/1389923/articlenumberlist-test1.csv\",\"configurationName\":\"Catalogue Download Oct 22\",\"customerID\":\"cust123456\",\"jobID\":\"onemd4cjobidojsubwhoym\",\"language\":\"en-EN\",\"region\":\"DE\",\"request\":[{\"pictures\":{\"productphoto\":{\"format\":[1,9,16,19,49,59,63,64,68],\"url\":\"false\"},\"dimensiondrawing\":{\"format\":[9,5,19,16,59,45,63,44],\"url\":\"true\"},\"model3D\":{\"format\":[9,51,19,16,73,11],\"url\":\"true\"},\"caeeplanMakro\":{\"format\":[69],\"url\":\"true\"},\"caeConnDiagram\":{\"format\":[51],\"url\":\"true\"},\"unitwiringdiagramIEC\":{\"format\":[19,5,16,9,59,45,44],\"url\":\"true\"},\"unitwiringdiagramANSI\":{\"format\":[19,5,16,9,59,44],\"url\":\"true\"},\"diagram2D\":{\"format\":[19,5,16,9,59,44],\"url\":\"true\"}}}]}}",
            "resultpayload": "{\"jobId\":\"onemd4cjobidojsubwhoym\",\"status\":\"finished incomplete\",\"statuscode\":41,\"statusmessage\":\"Package available, with the available assets (less than requested amount)\",\"progress\":100,\"ResultURL\":\"sftp://acapns.upload.akamai.com/1389923/provisioning_oneMD4C/onemd4cjobidojsubwhoym.zip\",\"LogURL\":\"sftp://acapns.upload.akamai.com/1389923/provisioning_oneMD4C/onemd4cjobidojsubwhoym.zip\",\"Request\":[{\"pictures\":{\"status\":\"finished incomplete\",\"statuscode\":41,\"statusmessage\":\"63 products exported out of 308\",\"errorcode\":\"\"}}]}",
            "articlenumberlistref": "sftp://acapns.upload.akamai.com/1389923/articlenumberlist-test1.csv",
            "resulturl": "sftp://acapns.upload.akamai.com/1389923/provisioning_oneMD4C/onemd4cjobidojsubwhoym.zip",
            "customerid": "cust123456",
            "language": "en-EN",
            "generators": 1,
            "lastmodified": "2023-06-12T08:50:37.190",
            "requestreceivedtimestamp": "2023-06-12T08:47:08.379",
            "requestcompletetimestamp": "2023-06-12T08:50:37.190",
            "statussenttimestamp": "2023-06-12T08:50:37.190",
            "sendattemptcount": 0
        }
    },
    {
        "Generator": {
            "id": 323,
            "snaplogicrequestid": "onemd4cjobidojsubwhoym",
            "generator": "BILDDB",
            "updatedfrom": "/SIE_DI_TEST/ACAP/Generator_Results/02_Process_Record",
            "runtimeurl": "https://cdn.elastic.snaplogic.com/sl/designer.html#pipe_runtime_id=6007d3c7c5702041dcf5166c_94d9478d-0773-4e1a-84c8-b63ef97da8dc",
            "totalproducts": 308,
            "status": "finished incomplete",
            "statuscode": 41,
            "statusmessage": "pictures have been partially exported",
            "errorcode": "",
            "exportedproducts": 63,
            "resulturl": "sftp://acapns.upload.akamai.com/1389923/delivery_Pictures/onemd4cjobidojsubwhoym.zip",
            "logurl": "sftp://acapns.upload.akamai.com/1389923/delivery_Pictures/onemd4cjobidojsubwhoym.zip",
            "lastmodified": "2023-06-12T08:50:23.044",
            "progress": 100
        }
    },
    {
        "GeneratorLog": {
            "snaplogicrequestid": "onemd4cjobidojsubwhoym",
            "logtype": "BIDDB Generator Status Update",
            "generator": "BILDDB",
            "updatedfrom": "/SIE_DI_TEST/ACAP/BILDDB_Subtasks/02_Process_Message",
            "lastmodified": "2023-06-12T08:49:35.242",
            "success": true,
            "responsestatus": null,
            "responseidentifier": "",
            "message": "request sent to generator",
            "requestpayload": "{\"jobId\":\"onemd4cjobidojsubwhoym#BILDDB\",\"status\":\"received\",\"exportedproducts\":0,\"statuscode\":20,\"statusmessage\":\"request sent to generator\",\"errorcode\":\"\",\"progress\":20,\"ResultURL\":\"\",\"LogURL\":\"\"}",
            "responsepayload": null,
            "runtimeurl": "https://cdn.elastic.snaplogic.com/sl/designer.html#pipe_runtime_id=6007d3c7c5702041dcf5166c_817d7616-5950-4159-bfa0-9854d0609f3e",
            "parentruntimeurl": "https://cdn.elastic.snaplogic.com/sl/designer.html#pipe_runtime_id=6007d3c7c5702041dcf5166c_a4106353-4f2f-4136-a4be-8ccb4b90ef89",
            "argsmetadata": "{\"SnaplogicRequestId\":\"onemd4cjobidojsubwhoym\",\"MLFBFilePath\":\"sftp://acapns.upload.akamai.com/1389923/articlenumberlist-test1.csv\"}"
        }
    },
    {
        "GeneratorLog": {
            "snaplogicrequestid": "onemd4cjobidojsubwhoym",
            "logtype": "BIDDB Generator Status Update",
            "generator": "BILDDB",
            "updatedfrom": "/SIE_DI_TEST/ACAP/BILDDB_Subtasks/02_Process_Message",
            "lastmodified": "2023-06-12T08:49:35.243",
            "success": true,
            "responsestatus": null,
            "responseidentifier": "",
            "message": "job accepted",
            "requestpayload": "{\"jobId\":\"onemd4cjobidojsubwhoym#BILDDB\",\"status\":\"in progress\",\"exportedproducts\":0,\"statuscode\":30,\"statusmessage\":\"job accepted\",\"errorcode\":\"\",\"progress\":30,\"ResultURL\":\"\",\"LogURL\":\"\"}",
            "responsepayload": null,
            "runtimeurl": "https://cdn.elastic.snaplogic.com/sl/designer.html#pipe_runtime_id=6007d3c7c5702041dcf5166c_817d7616-5950-4159-bfa0-9854d0609f3e",
            "parentruntimeurl": "https://cdn.elastic.snaplogic.com/sl/designer.html#pipe_runtime_id=6007d3c7c5702041dcf5166c_a4106353-4f2f-4136-a4be-8ccb4b90ef89",
            "argsmetadata": "{\"SnaplogicRequestId\":\"onemd4cjobidojsubwhoym\",\"MLFBFilePath\":\"sftp://acapns.upload.akamai.com/1389923/articlenumberlist-test1.csv\"}"
        }
    },
    {
        "GeneratorLog": {
            "snaplogicrequestid": "onemd4cjobidojsubwhoym",
            "logtype": "BIDDB Generator Status Update",
            "generator": "BILDDB",
            "updatedfrom": "/SIE_DI_TEST/ACAP/BILDDB_Subtasks/02_Process_Message",
            "lastmodified": "2023-06-12T08:49:35.243",
            "success": true,
            "responsestatus": null,
            "responseidentifier": "",
            "message": "job in work",
            "requestpayload": "{\"jobId\":\"onemd4cjobidojsubwhoym#BILDDB\",\"status\":\"in progress\",\"exportedproducts\":0,\"statuscode\":30,\"statusmessage\":\"job in work\",\"errorcode\":\"\",\"progress\":30,\"ResultURL\":\"\",\"LogURL\":\"\"}",
            "responsepayload": null,
            "runtimeurl": "https://cdn.elastic.snaplogic.com/sl/designer.html#pipe_runtime_id=6007d3c7c5702041dcf5166c_817d7616-5950-4159-bfa0-9854d0609f3e",
            "parentruntimeurl": "https://cdn.elastic.snaplogic.com/sl/designer.html#pipe_runtime_id=6007d3c7c5702041dcf5166c_a4106353-4f2f-4136-a4be-8ccb4b90ef89",
            "argsmetadata": "{\"SnaplogicRequestId\":\"onemd4cjobidojsubwhoym\",\"MLFBFilePath\":\"sftp://acapns.upload.akamai.com/1389923/articlenumberlist-test1.csv\"}"
        }
    },
    {
        "GeneratorLog": {
            "snaplogicrequestid": "onemd4cjobidojsubwhoym",
            "logtype": "BIDDB Generator Status Update",
            "generator": "BILDDB",
            "updatedfrom": "/SIE_DI_TEST/ACAP/BILDDB_Subtasks/02_Process_Message",
            "lastmodified": "2023-06-12T08:49:35.243",
            "success": true,
            "responsestatus": null,
            "responseidentifier": "",
            "message": "pictures have been partially exported",
            "requestpayload": "{\"jobId\":\"onemd4cjobidojsubwhoym#BILDDB\",\"status\":\"finished incomplete\",\"exportedproducts\":63,\"statuscode\":41,\"statusmessage\":\"pictures have been partially exported\",\"errorcode\":\"\",\"progress\":100,\"ResultURL\":\"sftp://acapns.upload.akamai.com/1389923/delivery_Pictures/onemd4cjobidojsubwhoym.zip\",\"LogURL\":\"sftp://acapns.upload.akamai.com/1389923/delivery_Pictures/onemd4cjobidojsubwhoym.zip\"}",
            "responsepayload": null,
            "runtimeurl": "https://cdn.elastic.snaplogic.com/sl/designer.html#pipe_runtime_id=6007d3c7c5702041dcf5166c_817d7616-5950-4159-bfa0-9854d0609f3e",
            "parentruntimeurl": "https://cdn.elastic.snaplogic.com/sl/designer.html#pipe_runtime_id=6007d3c7c5702041dcf5166c_a4106353-4f2f-4136-a4be-8ccb4b90ef89",
            "argsmetadata": "{\"SnaplogicRequestId\":\"onemd4cjobidojsubwhoym\",\"MLFBFilePath\":\"sftp://acapns.upload.akamai.com/1389923/articlenumberlist-test1.csv\"}"
        }
    }
]

In the meantime I found out it is not an array but an object (I believe).

I also tried

console.log("Status code: " + jsonData[0].Request.resultpayload.property("statuscode"));

but got an error
image

The problem is that resultpayload is an object in a string, therefore you need to parse this before you can work with it.

const response = pm.response.json();

console.log(response);

let resultpayload = JSON.parse(response[0].Request.resultpayload);

console.log(resultpayload);

console.log(resultpayload.statuscode);

This worked. Thank you very much for the help :grinning:

Hello. I have a follow-up question if you don’t mind.

I am now trying to log the properties within the Request object which is inside the resultpayload object.

I have tried both:

console.info(resultpayload.Request.statusmessage);
which gives undefined

and following the same logic as you showed:

let resultpayload2 =JSON.parse(resultpayload.Request);
console.info(resultpayload2.statusmessage);

which also gives an error.

I also tried a more simple approach with:

console.info("Trying this" + resultpayload.Request);

but it returns
image

Thank you once again for your help

The Request key is an array, so you need to target it with its index. (Array indexes start at 0)

image

let resultpayload = JSON.parse(response[0].Request.resultpayload);
console.log(resultpayload);
console.log(resultpayload.statuscode);
console.log(resultpayload.Request[0].pictures.statusmessage) // 63 products exported out of 308

So it was more straightforward than what I thought, I’ve now learnt that after parsing it once the properties can be accessed normally.

Thank you so much for your help once again :grinning: