如何使用 JSONata 更改 JSON 响应

发布于 2025-01-13 09:51:52 字数 1297 浏览 2 评论 0 原文

我正在使用返回以下 JSON 的 API:

{
  "rows": [
    {
      "keys": [
        "search term 1",
        "https://example.com/article-about-keyword-1/"
      ],
      "clicks": 24,
      "impressions": 54,
      "ctr": 0.4444444444444444,
      "position": 2.037037037037037
    },
    {
      "keys": [
        "search term 2",
        "https://example.com/article-about-keyword-2/"
      ],
      "clicks": 17,
      "impressions": 107,
      "ctr": 0.1588785046728972,
      "position": 2.663551401869159
    }
],
  "responseAggregationType": "byPage"
}

我正在尝试使用 JSONata 将其更改为更像这样的内容:

 {
  "rows": [
    {
      "keyword": search term 1,
      "URL": https://example.com/article-about-keyword-1/,
      "clicks": 24,
      "impressions": 54,
      "ctr": 0.4444444444444444,
      "position": 2.037037037037037
    },
    {
      "keyword": search term 2,
      "URL": https://example.com/article-about-keyword-2/,
      "clicks": 17,
      "impressions": 107,
      "ctr": 0.1588785046728972,
      "position": 2.663551401869159
    }
],
  "responseAggregationType": "byPage"
}

基本上,我正在尝试将“键”部分分解为“关键字”和“URL” '。

已经在 https://try.jsonata.org/ 玩了一段时间,但我不是走得很远。任何帮助表示赞赏。

I am working with an API that returns the following JSON:

{
  "rows": [
    {
      "keys": [
        "search term 1",
        "https://example.com/article-about-keyword-1/"
      ],
      "clicks": 24,
      "impressions": 54,
      "ctr": 0.4444444444444444,
      "position": 2.037037037037037
    },
    {
      "keys": [
        "search term 2",
        "https://example.com/article-about-keyword-2/"
      ],
      "clicks": 17,
      "impressions": 107,
      "ctr": 0.1588785046728972,
      "position": 2.663551401869159
    }
],
  "responseAggregationType": "byPage"
}

And I'm trying to use JSONata to change it to something more like this:

 {
  "rows": [
    {
      "keyword": search term 1,
      "URL": https://example.com/article-about-keyword-1/,
      "clicks": 24,
      "impressions": 54,
      "ctr": 0.4444444444444444,
      "position": 2.037037037037037
    },
    {
      "keyword": search term 2,
      "URL": https://example.com/article-about-keyword-2/,
      "clicks": 17,
      "impressions": 107,
      "ctr": 0.1588785046728972,
      "position": 2.663551401869159
    }
],
  "responseAggregationType": "byPage"
}

Basically, I'm trying the break the 'keys' part out into 'Keyword' and 'URL'.

Have been playing around for a while in https://try.jsonata.org/ but I'm not getting very far. Any help appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

失与倦" 2025-01-20 09:51:52

拆分键数组应该可以通过按索引访问每个元素来实现(假设关键字和 URL 保证出现在同一索引上)。

以下是从源文件转换为所需目标形状的完整 JSONata 表达式:

{
  "rows": rows.{
    "keyword": keys[0],
    "URL": keys[1],
    "clicks": clicks,
    "impressions": impressions,
    "ctr": ctr,
    "position": position 
  }[],
  "responseAggregationType": responseAggregationType
}

顺便说一句,我使用 映射工具,我的团队正在 Stedi 构建。

映射 UI

Splitting the keys array should be achievable by accessing each element there by its index (given that the keyword and the URL are guaranteed to appear on the same index).

Here’s the full JSONata expression to translate from your source file to the desired target shape:

{
  "rows": rows.{
    "keyword": keys[0],
    "URL": keys[1],
    "clicks": clicks,
    "impressions": impressions,
    "ctr": ctr,
    "position": position 
  }[],
  "responseAggregationType": responseAggregationType
}

By the way, I’ve built this solution in 2 minutes by using the Mappings tool that my team is building at Stedi.

Mappings UI

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文