The JSON data I am working on it. This is coming from Facebook ad API. This is being build for google data studio connector
Here is the JavaScript code I have used in google app script but it is showing error. I know it is totally wrong
const parseData = { "data": [{ "adcreatives": { "data": [{ "actor_id": "8834759718540", "id": "538" }] }, "insights": { "data": [{ "ad_id": "34536578578", "impressions": "89108", "actions": [{ "action_type": "comment", "value": "02" }, { "action_type": "post", "value": "03" } ], "date_start": "2022-06-11", "date_stop": "2022-07-10" }], "paging": { "cursors": { "before": "MAZDZD", "after": "MAZDZD" } } }, "created_time": "2022-06-10T22:59:33+0600", "id": "34536578578" }, { "adcreatives": { "data": [{ "actor_id": "7834759718970", "id": "342" }] }, "insights": { "data": [{ "ad_id": "238509545896206", "impressions": "57803", "actions": [{ "action_type": "post_engagement", "value": "2102" }, { "action_type": "page_engagement", "value": "03" } ], "date_start": "2022-06-11", "date_stop": "2022-07-10" }], "paging": { "cursors": { "before": "MAZDZD", "after": "MAZDZD" } } }, "created_time": "2022-06-11T22:59:33+0600", "id": "238509545896206" } ], "paging": { "cursors": { "before": "dfgdfgdfgdfsdgdfgdfgdfgdfgdgdg", "after": "yuyuyuyutyuytuyutytynfrgersggsgs" } } }; var data = { data: parseData.data.map(({ actions, ...rest }) => ({ ...rest, ...Object.fromEntries(actions.map(({ action_type, value }) => [action_type, value])) })) }; console.log(data);
Output should be like the following way so that I can get all the data in objective
{
"data": [
{
"actor_id": "8834759718540",
"id": "538",
"ad_id": "34536578578",
"impressions": "89108",
"comment": "02",
"post": "03",
"date_start": "2022-06-11",
"date_stop": "2022-07-10",
"created_time": "2022-06-10T22:59:33+0600"
},
{
"actor_id": "7834759718970",
"id": "342",
"ad_id": "238512373324806",
"impressions": "57803",
"post_engagement": "2102",
"page_engagement": "03",
"date_start": "2022-06-11",
"date_stop": "2022-07-10"
"created_time": "2022-06-11T22:59:33+0600"
}
],
}
Not sure how you suppose to handle the nested arrays, but for particular data this code does the job:
const parseData = { "data": [ { "adcreatives": { "data": [{ "actor_id": "8834759718540", "id": "538" }] }, "insights": { "data": [{ "ad_id": "34536578578", "impressions": "89108", "actions": [{ "action_type": "comment", "value": "02" }, { "action_type": "post", "value": "03" } ], "date_start": "2022-06-11", "date_stop": "2022-07-10" }], "paging": { "cursors": { "before": "MAZDZD", "after": "MAZDZD" } } }, "created_time": "2022-06-10T22:59:33+0600", "id": "34536578578" }, { "adcreatives": { "data": [{ "actor_id": "7834759718970", "id": "342" }] }, "insights": { "data": [{ "ad_id": "238509545896206", "impressions": "57803", "actions": [{ "action_type": "post_engagement", "value": "2102" }, { "action_type": "page_engagement", "value": "03" } ], "date_start": "2022-06-11", "date_stop": "2022-07-10" }], "paging": { "cursors": { "before": "MAZDZD", "after": "MAZDZD" } } }, "created_time": "2022-06-11T22:59:33+0600", "id": "238509545896206" } ], "paging": { "cursors": { "before": "dfgdfgdfgdfsdgdfgdfgdfgdfgdgdg", "after": "yuyuyuyutyuytuyutytynfrgersggsgs" } } }; var new_data = []; for (let obj of parseData.data) { var new_obj = {}; new_obj.actor_id = obj.adcreatives.data[0].actor_id; new_obj.id = obj.adcreatives.data[0].id; new_obj.ad_id = obj.insights.data[0].ad_id; new_obj.impressions = obj.insights.data[0].impressions; var actions = obj.insights.data[0].actions; actions.forEach(a => new_obj[a.action_type] = a.value) new_obj.date_start = obj.insights.data[0].date_start; new_obj.date_stop = obj.insights.data[0].date_stop; new_obj.created_time = obj.created_time; new_data.push(new_obj) } console.log(new_data);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.