?? Exploring JSON Data Extraction with Google Apps Script ??
Vikram Mohokar
DME | Data Analyst | MIS | Advance Excel | Google Sheets & Form | Looker Studio | Power BI | DAX | Google Workspace | Automation | Google Apps Script | ChatGPT | Gemini AI | Forecasting | HTML | CSS | Javascript
?? Exploring JSON Data Extraction with Google Apps Script ??
In the ever-evolving world of software development, mastering JSON data handling is a must-have skill. Recently, I worked on a project where I needed to extract specific data from a JSON object using Google Apps Script. Let me walk you through how I tackled this task, with some practical insights along the way!
Understanding the JSON Structure ??
The JSON data I was dealing with looked something like this:
{
"success": true,
"data": {
"count": "1",
"list": [
{
"id": 1,
"pid": "dc01968f-####-####-####-7cfcf51aa423",
"phone": "25312",
"created_at": "2020-04-02T06:47:50.229Z",
"updated_at": "2020-04-02T06:47:50.229Z",
"type": "outgoing",
"data": {
"body": {
"type": "message",
"user": {
"id": "[email protected]",
"name": "905301234567",
"phone": "905301234567"
},
"reply": "https://api.maytapi.com/api/dc01968f-####-####-####-7cfcf51aa423/12/sendMessage",
"message": {
"id": "[email protected]_***",
"text": "ok",
"type": "text",
"fromMe": true,
"_serialized": "[email protected]_***"
},
"phoneId": 1,
"phone_id": 1,
"receiver": "908501234567",
"productId": "dc01968f-####-####-####-7cfcf51aa423",
"timestamp": 1585808781,
"product_id": "dc01968f-####-####-####-7cfcf51aa423",
"conversation": "[email protected]"
},
"webhook": "YOUR WEBHOOK URL",
"response": {
"body": "OK",
"statusCode": 200
}
}
}
]
}
}
Extracting Specific Data with Google Apps Script ??
Using Google Apps Script, I extracted specific pieces of information from this JSON object. Here's the script I used:
领英推荐
function extractJsonData() {
const jsonResponse = {
"success": true,
"data": {
"count": "1",
"list": [
{
"id": 1,
"pid": "dc01968f-####-####-####-7cfcf51aa423",
"phone": "25312",
"created_at": "2020-04-02T06:47:50.229Z",
"updated_at": "2020-04-02T06:47:50.229Z",
"type": "outgoing",
"data": {
"body": {
"type": "message",
"user": {
"id": "[email protected]",
"name": "905301234567",
"phone": "905301234567"
},
"reply": "https://api.maytapi.com/api/dc01968f-####-####-####-7cfcf51aa423/12/sendMessage",
"message": {
"id": "[email protected]_***",
"text": "ok",
"type": "text",
"fromMe": true,
"_serialized": "[email protected]_***"
},
"phoneId": 1,
"phone_id": 1,
"receiver": "908501234567",
"productId": "dc01968f-####-####-####-7cfcf51aa423",
"timestamp": 1585808781,
"product_id": "dc01968f-####-####-####-7cfcf51aa423",
"conversation": "[email protected]"
},
"webhook": "YOUR WEBHOOK URL",
"response": {
"body": "OK",
"statusCode": 200
}
}
}
]
}
};
// Extract specific data
const listItem = jsonResponse.data.list[0];
// Extracting nested values
const userId = listItem.data.body.user.id;
const userName = listItem.data.body.user.name;
const messageText = listItem.data.body.message.text;
const receiverPhone = listItem.data.body.receiver;
const webhookUrl = listItem.data.webhook;
const responseBody = listItem.data.response.body;
// Log the extracted data
Logger.log('User ID: %s', userId);
Logger.log('User Name: %s', userName);
Logger.log('Message Text: %s', messageText);
Logger.log('Receiver Phone: %s', receiverPhone);
Logger.log('Webhook URL: %s', webhookUrl);
Logger.log('Response Body: %s', responseBody);
}
Key Takeaways ??
By following these steps, I successfully extracted the necessary information, significantly streamlining my workflow. Remember, the key to mastering JSON data extraction is practice and a clear understanding of the data structure. Happy coding! ??
Feel free to share your thoughts or ask any questions in the comments below! ??
#GoogleAppsScript #JSON #DataExtraction #CodingTips #DeveloperLife