匹配不同数组中的数值。

huangapple go评论60阅读模式
英文:

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 array dimensionValues (inside rows array);
  • Values from metricHeaders should match with values from array metricValues.

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"
    }
  }
}

huangapple
  • 本文由 发表于 2023年5月29日 20:10:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/76357249.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定