Rahul Pandey Rahul Pandey - 5 months ago 9
JSON Question

Parse json responsebody

I am trying to parse response body of rest request as mentioned below

responsebody log is

{
"kind": "drive#file",
"id": "abcdefg..............",
"etag": "\"abcdefg..............\"",
"selfLink": "https://www.googleapis.com/drive/v2/files/abcdefg..............",
"alternateLink": "https://docs.google.com/document/d/abcdefg............../edit?usp=drivesdk",
"embedLink": "https://docs.google.com/document/d/abcdefg............../preview",
"iconLink": "https://ssl.gstatic.com/docs/doclist/images/icon_11_document_list.png",
"title": "test.docx",
"mimeType": "application/vnd.google-apps.document",
"labels": {
"starred": false,
"hidden": false,
"trashed": false,
"restricted": false,
"viewed": true
},
"createdDate": "2016-06-16T08:12:32.075Z",
"modifiedDate": "2016-06-16T08:12:32.075Z",
"modifiedByMeDate": "2016-06-16T08:12:32.075Z",
"lastViewedByMeDate": "2016-06-16T08:12:32.075Z",
"markedViewedByMeDate": "1970-01-01T00:00:00.000Z",
"version": "11300",
"parents": [
{
"kind": "drive#parentReference",
"id": "0AFFGSblfddqtUk9PVA",
"selfLink": "https://www.googleapis.com/drive/v2/files/abcdefg............../parents/0AFFGSblfddqtUk9PVA",
"parentLink": "https://www.googleapis.com/drive/v2/files/abcdefg..............",
"isRoot": true
}
],
"exportLinks": {
"application/rtf": "https://docs.google.com/feeds/download/documents/export/Export?id=abcdefg..............&exportFormat=rtf",
"application/vnd.oasis.opendocument.text": "https://docs.google.com/feeds/download/documents/export/Export?id=abcdefg..............&exportFormat=odt",
"text/html": "https://docs.google.com/feeds/download/documents/export/Export?id=abcdefg..............&exportFormat=html",
"application/pdf": "https://docs.google.com/feeds/download/documents/export/Export?id=abcdefg..............&exportFormat=pdf",
"application/zip": "https://docs.google.com/feeds/download/documents/export/Export?id=abcdefg..............&exportFormat=zip",
"application/vnd.openxmlformats-officedocument.wordprocessingml.document": "https://docs.google.com/feeds/download/documents/export/Export?id=abcdefg..............&exportFormat=docx",
"text/plain": "https://docs.google.com/feeds/download/documents/export/Export?id=abcdefg..............&exportFormat=txt"
},
"userPermission": {
"kind": "drive#permission",
"etag": "\"abcdefg..............\"",
"id": "me",
"selfLink": "https://www.googleapis.com/drive/v2/files/abcdefg............../permissions/me",
"role": "owner",
"type": "user"
},
"quotaBytesUsed": "0",
"ownerNames": [
"Rahul Pandey"
],
"owners": [
{
"kind": "drive#user",
"displayName": "Rahul Pandey",
"picture": {
"url": "https://lh3.googleusercontent.com/abcdefg............../photo.jpg"
},
"isAuthenticatedUser": true,
"permissionId": "abcdefg..............",
"emailAddress": "abcdefg.............."
}
],
"lastModifyingUserName": "Rahul Pandey",
"lastModifyingUser": {
"kind": "drive#user",
"displayName": "Rahul Pandey",
"picture": {
"url": "abcdefg.............."
},
"isAuthenticatedUser": true,
"permissionId": "abcdefg..............",
"emailAddress": "abcdefg.............."
},
"editable": true,
"copyable": true,
"writersCanShare": true,
"shared": false,
"explicitlyTrashed": false,
"appDataContents": false,
"spaces": [
"drive"
]
}


I can parse file id by below method

var json1 = responseUpload.getBody();
var obj1 = JSON.parse(json1);
gs.log("the file id is "+obj1.id);


Even, I can parse parents ID by below method

var json1 = responseUpload.getBody();
var obj1 = JSON.parse(json1);
gs.log("the parent id is "+obj1.parent[0].id);


However when I am trying to export link

gs.log("the file id is "+obj1.exportLinks.application/rtf);


It is not printing/undefined value.

Anyone can help me out on this ? I am looking out for native js method.

Answer

You need to be using the bracket notation for this: obj1.exportLinks["application/rtf"]. This is so because the /symbol is used for division in JavaScript.

Comments