首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在用JSONata重组数据方面需要帮助

在用JSONata重组数据方面需要帮助
EN

Stack Overflow用户
提问于 2022-11-04 11:44:09
回答 1查看 29关注 0票数 -2

我做不到,这里需要一些社区的智慧。请在下面找到源数据和期望的结果。我目前正在熟悉JSONata。

我在JSONata文档中尝试了几乎所有的组合,但是我得到了奇怪的结果。

源数据

代码语言:javascript
复制
[
  {
    "id": 784521,
    "name": "T-Shirt - Red / 60 XXL",
    "properties": [
      {
        "name": "customization",
        "value": "Text, Logo"
      },
      {
        "name": "customization_text",
        "value": "John Doe"
      },
      {
        "name": "customization_text_pos",
        "value": "Left"
      },
      {
        "name": "customization_logo",
        "value": "https://https://picsum.photos/200/300"
      },
      {
        "name": "customization_logo_pos",
        "value": "Right"
      }
    ],
    "quantity": 8,
    "sku": "888-111"
  },{
    "id": 154857,
    "name": "Pullover - Blue / 48 L",
    "properties": [
      {
        "name": "customization",
        "value": "Text"
      },
      {
        "name": "customization_text",
        "value": "John Doe"
      },
      {
        "name": "customization_text_pos",
        "value": "Right"
      }
    ],
    "quantity": 4,
    "sku": "555-111"
  }
]

期望结果

代码语言:javascript
复制
{
  "products": [
    {
      "name": "T-Shirt - Red / 60 XXL",
      "quantity": 8,
      "customization": "Text, Logo",
      "customization_text": "John Doe",
      "customization_text_pos": "Left",
      "customization_logo": "https://https://picsum.photos/200/300",
      "customization_logo_pos": "Right"
    },
    {
      "name": "Pullover - Blue / 48 L",
      "quantity": 4,
      "customization": "Text",
      "customization_text": "John Doe",
      "customization_text_pos": "Right",
      "customization_logo": "",
      "customization_logo_pos": ""
    }
  ]
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-11-04 12:08:00

我不确定用空字符串值填充丢失的属性有多重要,但如果不是--将属性数组还原为对象,则可以实现以下结果:

代码语言:javascript
复制
{
  "products": $$.(
    $item := $;
    
    properties{
      "name": $item.name,
      "quantity": $item.quantity,
      name: value 
    }
  )
}

现场观看:https://stedi.link/6htarTz

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/74316521

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档