From this JSON below:
{
"query":{
"ids":[
95445875
],
"group": "day",
"dimensions":[
"ym:s:date",
"ym:s:lastSignUTMSource",
"ym:s:lastSignUTMMedium",
"ym:s:lastSignUTMCampaign",
"ym:s:lastSignUTMContent",
"ym:s:lastSignUTMTerm"
],
"metrics":[
"ym:s:goal315094773visits",
"ym:s:goal318656867visits"
]
},
"data":[
{
"dimensions":[
{
"name":"2024-01-12"
},
{
"name":"5ka"
},
{
"name":"SITE"
},
{
"name":"5ka_HR_nov23"
},
{
"name":"button"
},
{
"name":"click"
}
],
"metrics":[
64.0,
2.0
]
},
{
"dimensions":[
{
"name":"2024-01-13"
},
{
"name":"5ka"
},
{
"name":"SITE"
},
{
"name":"5ka_HR_nov23"
},
{
"name":"button"
},
{
"name":"click"
}
],
"metrics":[
13.0,
0.0
]
},
{
"dimensions":[
{
"name":"2024-01-13"
},
{
"name":"Yandex"
},
{
"name":"SEM"
},
{
"name":"5ka_HR_oct-dec'23"
},
{
"name":"TGB"
},
{
"name":"shop"
}
],
"metrics":[
5.0,
4.0
]
},
{
"dimensions":[
{
"name":"2024-01-12"
},
{
"name":"Yandex"
},
{
"name":"SEM"
},
{
"name":"5ka_HR_oct-dec'23"
},
{
"name":"TGB"
},
{
"name":"shop"
}
],
"metrics":[
3.0,
0.0
]
}
],
"total_rows":4
}
I expect to get this result:
[
{
"counter_id":95445875,
"date_of_visit":"2024-01-12",
"utm_source":"5ka",
"utm_medium":"SITE",
"utm_campaign":"5ka_HR_nov23",
"utm_content":"button",
"utm_term":"click",
"goal_id":"315094773",
"conversions":64.0
},
{
"counter_id":95445875,
"date_of_visit":"2024-01-12",
"utm_source":"5ka",
"utm_medium":"SITE",
"utm_campaign":"5ka_HR_nov23",
"utm_content":"button",
"utm_term":"click",
"goal_id":"318656867",
"conversions":2.0
},
{
"counter_id":95445875,
"date_of_visit":"2024-01-13",
"utm_source":"5ka",
"utm_medium":"SITE",
"utm_campaign":"5ka_HR_nov23",
"utm_content":"button",
"utm_term":"click",
"goal_id":"315094773",
"conversions":13.0
},
{
"counter_id":95445875,
"date_of_visit":"2024-01-13",
"utm_source":"5ka",
"utm_medium":"SITE",
"utm_campaign":"5ka_HR_nov23",
"utm_content":"button",
"utm_term":"click",
"goal_id":"318656867",
"conversions":0.0
},
{
"counter_id":95445875,
"date_of_visit":"2024-01-12",
"utm_source":"Yandex",
"utm_medium":"SEM",
"utm_campaign":"5ka_HR_oct-dec'23",
"utm_content":"TGB",
"utm_term":"shop",
"goal_id":"315094773",
"conversions":5.0
},
{
"counter_id":95445875,
"date_of_visit":"2024-01-12",
"utm_source":"Yandex",
"utm_medium":"SEM",
"utm_campaign":"5ka_HR_oct-dec'23",
"utm_content":"TGB",
"utm_term":"shop",
"goal_id":"318656867",
"conversions":4.0
},
{
"counter_id":95445875,
"date_of_visit":"2024-01-13",
"utm_source":"Yandex",
"utm_medium":"SEM",
"utm_campaign":"5ka_HR_oct-dec'23",
"utm_content":"TGB",
"utm_term":"shop",
"goal_id":"315094773",
"conversions":3.0
},
{
"counter_id":95445875,
"date_of_visit":"2024-01-13",
"utm_source":"Yandex",
"utm_medium":"SEM",
"utm_campaign":"5ka_HR_oct-dec'23",
"utm_content":"TGB",
"utm_term":"shop",
"goal_id":"318656867",
"conversions":0.0
}
]
Explanation
From souce JSON I need:
- id from
query.ids
array and call new propertycounter_id
- compare and join values from array
query.dimensions
and values from arraydata.dimensions
, but give them new names. Values fromquery.dimensions
should be property names for values fromdata.dimensions
(we need this to keep right order). After this I want to change these names:ym:s:date
should bedate_of_visit
,ym:s:lastSignUTMSource
should beutm_source
,ym:s:lastSignUTMMedium
toutm_medium
,ym:s:lastSignUTMCampaign
toutm_campaign
,ym:s:lastSignUTMContent
toutm_content
,ym:s:lastSignUTMTerm
toutm_term
. - compare and join values from
query.metrics
with values from arraydata.metrics
. But each element from arrayquery.metrics
. f.e.ym:s:goal315094773visits
should be written asgoal_id
: 315094773 (we need value between goal….visits). And value fromdata.metrics
should be written asconversions
.
So I expect to get result JSON like above presented.
I hard worked with it and got this JOLT config, but it doesn’t even work.
My knowledge is definetely not enough(
2
Answers
Not sure if this is the most efficient way but it does the job and its dynamic regardless how many query dimensions you have or how many data metrics you have. Only condition is the query dimension order coincide with data dimension order to match each value with its field based on that order:
Let me know if you have any questions.
Thanks
You can flatten by the following transformation :