英文:
Match values from different arrays
问题
我从Google Analytics API收到JSON。它看起来像这样:
{
"dimensionHeaders": [
{
"name": "date"
},
{
"name": "sessionMedium"
},
{
"name": "sessionCampaignName"
}
],
"metricHeaders": [
{
"name": "sessions"
},
{
"name": "bounceRate"
},
{
"name": "newUsers"
}
],
"rows": [
{
"dimensionValues": [
{
"value": "20230526"
},
{
"value": "organic"
},
{
"value": "RV_olov_23"
}
],
"metricValues": [
{
"value": "47"
},
{
"value": "1"
},
{
"value": "40"
}
]
},
{
"dimensionValues": [
{
"value": "20230527"
},
{
"value": "online_plus"
},
{
"value": "Borhoni_aqua_23_may"
}
],
"metricValues": [
{
"value": "31"
},
{
"value": "0.25"
},
{
"value": "10"
}
]
}
],
"rowCount": 2
}
我需要执行复杂的操作。以...Headers
结尾的数组应与以....Values
结尾的数组匹配。所以,我们应该确切比较什么:
dimensionHeaders
数组中的Values应与rows
数组中的dimensionValues值匹配;metricHeaders
中的Values应与metricValues
数组中的值匹配。
最后,这意味着...Headers
数组中的values是键名,...Values
数组中的values是这些新键的values。
所以让我们看看我们应该得到什么:
[
{
"date": "20230526",
"sessionMedium": "organic",
"sessionCampaignName": "RV_olov_23",
"sessions": 47,
"bounceRate": 1,
"newUsers": 40
},
{
"date": "20230527",
"sessionMedium": "online_plus",
"sessionCampaignName": "Borhoni_aqua_23_may",
"sessions": 31,
"bounceRate": 0.25,
"newUsers": 10
}
]
我不确定是否可以使用JOLT来实现这个目标。对于任何帮助,我将不胜感激!
英文:
I receive JSON from Google Analytics API. It looks like this:
{
"dimensionHeaders": [
{
"name": "date"
},
{
"name": "sessionMedium"
},
{
"name": "sessionCampaignName"
}
],
"metricHeaders": [
{
"name": "sessions"
},
{
"name": "bounceRate"
},
{
"name": "newUsers"
}
],
"rows": [
{
"dimensionValues": [
{
"value": "20230526"
},
{
"value": "organic"
},
{
"value": "RV_olov_23"
}
],
"metricValues": [
{
"value": "47"
},
{
"value": "1"
},
{
"value": "40"
}
]
},
{
"dimensionValues": [
{
"value": "20230527"
},
{
"value": "online_plus"
},
{
"value": "Borhoni_aqua_23_may"
}
],
"metricValues": [
{
"value": "31"
},
{
"value": "0.25"
},
{
"value": "10"
}
]
}
],
"rowCount": 2
}
I need to perform complex manipulations. Arrays ending with ...Headers
should match with arrays ending with ....Values
. So, what should we exactly compare:
- Values from array
dimensionHeaders
should match with values from arraydimensionValues
(insiderows
array); - Values from
metricHeaders
should match with values from arraymetricValues
.
And finally, it means that values from ...Headers
arrays - are keys names and values from ...Values
arrays - are values for these new keys.
So let's see what we should get:
[
{
"date": "20230526",
"sessionMedium": "organic",
"sessionCampaignName": "RV_olov_23",
"sessions": 47,
"bounceRate": 1,
"newUsers": 40
},
{
"date": "20230527",
"sessionMedium": "online_plus",
"sessionCampaignName": "Borhoni_aqua_23_may",
"sessions": 31,
"bounceRate": 0.25,
"newUsers": 10
}
]
I don't know exactly if is it possible to do with JOLT or not. I will be grateful for any help!
答案1
得分: 2
你可以使用以下规范:
[
{
"operation": "shift",
"spec": {
"rows": {
"*": {
"dimensionValues": {
"*": {
"value": "[&3].@(^,dimensionHeaders[&1].name)"
}
},
"metricValues": {
"*": {
"value": "[&3].@(^,metricHeaders[&1].name)"
}
}
}
}
}
}
]
如果你需要将字符串数字转换为整数类型,你可以将以下规范添加到上述规范中:
{
"operation": "modify-overwrite-beta",
"spec": {
"*": {
"sessions": "=toInteger",
"bounceRate": "=toDouble",
"newUsers": "=toInteger"
}
}
}
英文:
You can use this spec:
[
{
"operation": "shift",
"spec": {
"rows": {
"*": {
"dimensionValues": {
"*": {
"value": "[&3].@(5,dimensionHeaders[&1].name)"
}
},
"metricValues": {
"*": {
"value": "[&3].@(5,metricHeaders[&1].name)"
}
}
}
}
}
}
]
If you need to change your string numbers to integer type you can add the following spec to the above spec:
{
"operation": "modify-overwrite-beta",
"spec": {
"*": {
"sessions": "=toInteger",
"bounceRate": "=toDouble",
"newUsers": "=toInteger"
}
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论