当前位置: 首页 > 知识库问答 >
问题:

Jolt转换嵌套json数组

夔建章
2023-03-14

我有json,其中包括多个产品,每个产品有多个不同的细节。使用jolt,我只需要输入json中的几个字段,遵循与输入json几乎相同的结构。我成功地迭代了产品,但是当我试图迭代每个产品变体时,我没有得到想要的输出。

输入. json

{
  "products": [
    {
      "id": 6635020550275,
      "title": "UAT-9122021",
      "handle": "uat-9122021",
      "body_html": "UAT-9122021",
      "published_at": "2021-09-13T20:56:30+10:00",
      "created_at": "2021-09-13T20:56:28+10:00",
      "updated_at": "2021-09-13T21:05:17+10:00",
      "vendor": "Britax Test",
      "product_type": "",
      "tags": [],
      "variants": [
        {
          "id": 39516385214595,
          "title": "Default Title",
          "option1": "Default Title",
          "option2": null,
          "option3": null,
          "sku": "UAT-9122021",
          "requires_shipping": true,
          "taxable": true,
          "featured_image": null,
          "available": true,
          "price": "19.30",
          "grams": 0,
          "compare_at_price": null,
          "position": 1,
          "product_id": 6635020550275,
          "created_at": "2021-09-13T20:56:29+10:00",
          "updated_at": "2021-09-13T21:04:45+10:00"
        }
      ],
      "images": [],
      "options": [
        {
          "name": "Title",
          "position": 1,
          "values": [
            "Default Title"
          ]
        }
      ]
    },
    {
      "id": 6632446787715,
      "title": "UAT-992021",
      "handle": "uat-992021",
      "body_html": "UAT-992021",
      "published_at": "2021-09-09T22:53:05+10:00",
      "created_at": "2021-09-09T22:53:04+10:00",
      "updated_at": "2021-09-09T23:05:26+10:00",
      "vendor": "Britax Test",
      "product_type": "",
      "tags": [],
      "variants": [
        {
          "id": 39511546462339,
          "title": "Default Title",
          "option1": "Default Title",
          "option2": null,
          "option3": null,
          "sku": "UAT-992021",
          "requires_shipping": true,
          "taxable": true,
          "featured_image": null,
          "available": true,
          "price": "35.50",
          "grams": 0,
          "compare_at_price": null,
          "position": 1,
          "product_id": 6632446787715,
          "created_at": "2021-09-09T22:53:04+10:00",
          "updated_at": "2021-09-09T23:04:40+10:00"
        }
      ],
      "images": [],
      "options": [
        {
          "name": "Title",
          "position": 1,
          "values": [
            "Default Title"
          ]
        }
      ]
    }
  ]
}

这里是Spec.json

[
  {
    "operation": "shift",
    "spec": {
      "products": {
        "*": {
          "created_at": "productDoc[&1].createdDateTime",
          "id": "productDoc[&1].id",
          "variants": {
            "*": {
              "id": "productDoc[&1].variants.[&1].ecommVariantId",
              "created_at": "productDoc[&1].variants.[&1].createdDateTime"
            }
          }
        }
      }
    }
  }
]

我想要的预期输出。

{
    "productDoc": [{
        "createdDateTime": "2021-09-13T20:56:28+10:00",
        "id": 6635020550275,
        "variants": [{
            "ecommVariantId": [39516385214595],
            "createdDateTime": ["2021-09-13T20:56:29+10:00"]
        }]
    }, 
    {
        "createdDateTime": "2021-09-09T22:53:04+10:00",
        "id": 6632446787715,
        "variants": [{
            "ecommVariantId": [39516385214595],
            "createdDateTime": ["2021-09-09T22:53:04+10:00"]
        }]
    }]
}

我现在得到的实际输出。

{
  "productDoc" : [ {
    "createdDateTime" : "2021-09-13T20:56:28+10:00",
    "id" : 6635020550275,
    "variants" : [ {
      "ecommVariantId" : [ 39516385214595, 39511546462339 ],
      "createdDateTime" : [ "2021-09-13T20:56:29+10:00", "2021-09-09T22:53:04+10:00" ]
    } ]
  }, {
    "createdDateTime" : "2021-09-09T22:53:04+10:00",
    "id" : 6632446787715
  } ]
}

共有1个答案

呼延钱明
2023-03-14

不要使用[

json prettyprint-override">[
  {
    "operation": "shift",
    "spec": {
      "products": {
        "*": {
          "created_at": "productDoc[&1].createdDateTime",
          "id": "productDoc[&1].&",
          "variants": {
            "*": {
              "id": "productDoc[&3].&2.[&1].ecommVariantId",
              "created_at": "productDoc[&3].&2.[&1].createdDateTime"
            }
          }
        }
      }
    }
  }
]
 类似资料:
  • 我有一个JSON如下所示: 为什么在输出中看不到Level1、Level2?请有人帮忙,我想看看在输出和输入太相似了。

  • 如何使用jolt转换将平面JSON转换为嵌套JSON?我对JSON和jolt是新手。 输入: 预期输出: 编辑:我想在< code>SubFunds中添加一个新字段,但是新字段不在JSON文件中,它是计算字段,我可以按原样添加示例吗 并重命名字段:

  • 我有一个嵌套的JSON对象,如下所示: 我想将其转换为: 我如何使用JOLT实现这一点?感谢您的参与。

  • 我只想扁平化嵌套JSON的属性,但仍然适用于输入数组中的所有对象 很难将这三个字段放在一个规范中(类型字段、geo字段、properties字段)。我编写了规范来单独完成每一个操作,但是当我将这些规范组合在一个对象中使用时,它会产生错误的输出--对象数组真的把它搞砸了。 期望输出:

  • 所以目前我看到的是这样的: 这是我目前编写的Jolt规范(编辑): 以及转换后的输出: 任何帮助都是非常感谢的。

  • 我正在尝试编写一个规范来使用jolt转换进行以下转换。我只对更改json中键的名称感兴趣,值应该保持不变。帮帮我。 输入Json: 预期输出: