0

http://hubtest.atspace.cc/json.html

in the above JSON response, I need to obtain the LAST objectId value to use as part of another URL. What is the best way to do this with javascript?

I attempted it with regex, but it got too messy. I'm assuming there is an easier way.

2 Answers 2

3

Try JSON.parse(...) on the source (since it is literally JSON) and then just treat it like an object literal.

Since it's an array you can do var array = JSON.parse(...) then get array[array.length - 1].objectID.

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

Comments

1

Basically,

var lastObj = a[a.length - 1];

returns back the last object in the array. Then, you can access its properties;

var objectID = lastObj['objectId'];

Shortcut

var objectID = a[a.length - 1]['objectId'];

Array.length returns total elements in array, and because numbers are zero-based, taking 1 off the length gets the last index. eg. for an array with 6 elements, the last element index is 5 (6-1) = 5 )

Working Snippet, I trimmed objects down to 2.

   var a = [{
       "type": "PAPER",
       "data": null,
       "proceedingNumber": "IPR2016-00546",
       "petitionId": "1463786",
       "mimeType": null,
       "fileName": "'306 Petition.pdf",
       "partyGroupType": "petitioner",
       "proceedingPartyGroupId": null,
       "availability": "PUBLIC",
       "documentName": "Petition For Inter Partes Review U.S. Patent No. 8,772,306",
       "pageCount": "0",
       "documentType": "16",
       "exhibitNumber": null,
       "petitionVO": null,
       "institutionDecisionVO": null,
       "terminationDecisionVO": null,
       "proceedingReqType": null,
       "proceedingReqTypeId": null,
       "proceedingReqTypeStatusId": null,
       "appealId": null,
       "internalUserProxyEmail": null,
       "proceedingId": "1463786",
       "paperType": "16",
       "documentTypeId": 16,
       "customMotionTypeName": null,
       "otherMotionType": null,
       "objectId": "d29ya3NwYWNlOi8vU3BhY2VzU3RvcmUvNGVjOGFjMzQtODI5Yi00OTZhLTg0ZDItMDU2NTQzNmQ4NTI0OzEuMA==",
       "objectType": null,
       "artifactSubmissionId": "84644821",
       "exhibitSequenceNumber": null,
       "dateAdded": "02/02/2016",
       "uploadStatus": null,
       "expungedFlag": "N",
       "deletedFlag": null,
       "docVersionLabel": null,
       "filingDate": "02/02/2016",
       "proceedingArtifactId": "169264898",
       "artifactNumber": "1",
       "petitionState": null,
       "patentNumber": null,
       "fileSize": 0,
       "submitterId": 11915,
       "comment": null,
       "createdbyAuthorName": null,
       "disableSelect": null,
       "employeeId": null,
       "lockControlNo": 0,
       "paperTypeName": "Petition",
       "proceedingPartyId": null,
       "filingParty": "petitioner",
       "documentCategory": null,
       "showExpungeAction": false,
       "showUnExpungeAction": false,
       "showDownloadLink": true,
       "skipUploadTaskRecord": false,
       "showEditLink": true,
       "inputStream": null
   }, {
       "type": "PAPER",
       "data": null,
       "proceedingNumber": "IPR2016-00546",
       "petitionId": "1463786",
       "mimeType": null,
       "fileName": "306 Exhibit List.pdf",
       "partyGroupType": "petitioner",
       "proceedingPartyGroupId": null,
       "availability": "PUBLIC",
       "documentName": "Exhibit List",
       "pageCount": "0",
       "documentType": "16",
       "exhibitNumber": null,
       "petitionVO": null,
       "institutionDecisionVO": null,
       "terminationDecisionVO": null,
       "proceedingReqType": null,
       "proceedingReqTypeId": null,
       "proceedingReqTypeStatusId": null,
       "appealId": null,
       "internalUserProxyEmail": null,
       "proceedingId": "1463786",
       "paperType": "16",
       "documentTypeId": 16,
       "customMotionTypeName": null,
       "otherMotionType": null,
       "objectId": "d29ya3NwYWNlOi8vU3BhY2VzU3RvcmUvNDBhMjRjMmQtYWZkZi00OTdlLThkN2ItZGQ2ZTE5MmVjMWVkOzEuMA==",
       "objectType": null,
       "artifactSubmissionId": "84644822",
       "exhibitSequenceNumber": null,
       "dateAdded": "02/02/2016",
       "uploadStatus": null,
       "expungedFlag": "N",
       "deletedFlag": null,
       "docVersionLabel": null,
       "filingDate": "02/02/2016",
       "proceedingArtifactId": "169264900",
       "artifactNumber": "2",
       "petitionState": null,
       "patentNumber": null,
       "fileSize": 0,
       "submitterId": 11915,
       "comment": null,
       "createdbyAuthorName": null,
       "disableSelect": null,
       "employeeId": null,
       "lockControlNo": 0,
       "paperTypeName": "Petition",
       "proceedingPartyId": null,
       "filingParty": "petitioner",
       "documentCategory": null,
       "showExpungeAction": false,
       "showUnExpungeAction": false,
       "showDownloadLink": true,
       "skipUploadTaskRecord": false,
       "showEditLink": true,
       "inputStream": null
   }]
console.log(a[a.length - 1]['objectId']);

1 Comment

This answer needs information about how to parse the JSON. Notice that the OP has pointed to JSON output, and is mentioning the possibility of using RegEx to access the desired bits.

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.