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

使用jolt转换将平面json转换为多个有效载荷的嵌套json

洪高扬
2023-03-14

我试图写一个规范来使用jolt转换完成下面的转换。我需要将平面JSON转换成嵌套JSON。

输入数据:

[
  {
    "container_id": "ABC_id",
    "shipperN": null,
    "PNumber": null,
    "trackingNumber": null,
    "loadNumber": "BO123345",
    "billOfLading": "BO12345",
    "addressLine1": "ABC Street",
    "city": "ABC_city",
    "country": "ABC_country",
    "earliestAppointmentTime": "XXXXX09:25",
    "postalCode": "XXXX3",
    "sequence": "1",
    "state": "ABC_state",
    "stopReferenceId": "0001",
    "stopType": "PU",
    "containerNumber": "232323"
  },
  {
    "container_id": "ABC_id",
    "shipperN": null,
    "PNumber": null,
    "trackingNumber": null,
    "loadNumber": "BO123345",
    "billOfLading": "BO12345",
    "addressLine1": null,
    "city": "ABC1_city",
    "country": "ABC1_country",
    "earliestAppointmentTime": "XXXXX09:15",
    "postalCode": "XXXX4",
    "sequence": "2",
    "state": "ABC1_state",
    "stopReferenceId": "0002",
    "stopType": "PL",
    "containerNumber": "232323"
  },
  {
    "container_id": "DEF_id",
    "shipperN": null,
    "PNumber": null,
    "trackingNumber": null,
    "loadNumber": "DO123345",
    "billOfLading": "DO12345",
    "addressLine1": null,
    "city": "DEF_city",
    "country": "DEF_country",
    "earliestAppointmentTime": "XXXXX08:15",
    "postalCode": "XXXX5",
    "sequence": "4",
    "state": "DEF_state",
    "stopReferenceId": "0003",
    "stopType": "PU",
    "containerNumber": "454545"
  },
  {
    "container_id": "DEF_id",
    "shipperN": null,
    "PNumber": null,
    "trackingNumber": null,
    "loadNumber": "DO123345",
    "billOfLading": "DO12345",
    "addressLine1": "DEF_address",
    "city": "DEF1_city",
    "country": "DEF_country",
    "earliestAppointmentTime": "XXXXX07:15",
    "postalCode": "XXXX6",
    "sequence": "5",
    "state": "DEF_state",
    "stopReferenceId": "0004",
    "stopType": "PL",
    "containerNumber": "454545"
  }
]

我在将平面JSON转换为嵌套JSON时遇到了一些问题。这里,我希望基于stoptype属性聚合数据,并且需要针对唯一的有效负载进行聚合。我用https://jolt-demo.appspot.com来测试以下内容。

输出:

json prettyprint-override">[
  {
    "container_id": "ABC_id",
    "shipperN": null,
    "PNumber": null,
    "trackingNumber": null,
    "loadNumber": "BO123345",
    "billOfLading": "BO12345",
    "PU": {
      "addressLine1": "ABC Street",
      "city": "ABC_city",
      "country": "ABC_country",
      "earliestAppointmentTime": "XXXXX09:25",
      "postalCode": "XXXX3",
      "sequence": "1",
      "state": "ABC_state",
      "stopReferenceId": "0001",
      "stopType": "PU"
    },
    "PL": {
      "addressLine1": null,
      "city": "ABC1_city",
      "country": "ABC1_country",
      "earliestAppointmentTime": "XXXXX09:15",
      "postalCode": "XXXX4",
      "sequence": "2",
      "state": "ABC1_state",
      "stopReferenceId": "0002",
      "stopType": "PL"
    },
    "containerNumber": "232323"
  },
  {
    "container_id": "DEF_id",
    "shipperN": null,
    "PNumber": null,
    "trackingNumber": null,
    "loadNumber": "DO123345",
    "billOfLading": "DO12345",
    "PU": {
      "addressLine1": null,
      "city": "DEF_city",
      "country": "DEF_country",
      "earliestAppointmentTime": "XXXXX08:15",
      "postalCode": "XXXX5",
      "sequence": "4",
      "state": "DEF_state",
      "stopReferenceId": "0003",
      "stopType": "PU"
    },
    "PL": {
      "addressLine1": "DEF_address",
      "city": "DEF1_city",
      "country": "DEF1_country",
      "earliestAppointmentTime": "XXXXX07:15",
      "postalCode": "XXXX6",
      "sequence": "5",
      "state": "DEF_state",
      "stopReferenceId": "0004",
      "stopType": "PL"
    },
    "containerNumber": "454545"
  }
]

你能帮我完成这个预期的输出吗?

共有1个答案

华鹭洋
2023-03-14

诀窍是按照< code>@(1,container_id)和< code>@(1,stopType)对所有属性进行分区,以便将这些属性嵌套在由< code>stopType属性值确定的子对象中,例如

[
  {
    // separate by @(1,container_id) and @(1,stopType) for attributes to be nested
    "operation": "shift",
    "spec": {
      "*": {
        "container_id": "@(1,container_id).&",
        "shipperN": "@(1,container_id).&",
        "PNumber": "@(1,container_id).&",
        "trackingNumber": "@(1,container_id).&",
        "loadNumber": "@(1,container_id).&",
        "billOfLading": "@(1,container_id).&",
        "addressLine1": "@(1,container_id).@(1,stopType).&",
        "city": "@(1,container_id).@(1,stopType).&",
        "country": "@(1,container_id).@(1,stopType).&",
        "earliestAppointmentTime": "@(1,container_id).@(1,stopType).&",
        "postalCode": "@(1,container_id).@(1,stopType).&",
        "sequence": "@(1,container_id).@(1,stopType).&",
        "state": "@(1,container_id).@(1,stopType).&",
        "stop*": "@(1,container_id).@(1,stopType).&",
        "containerNumber": "@(1,container_id).&"
      }
    }
  },
  {
    // reduce only to a single one for the repeating components of the arrays
    "operation": "cardinality",
    "spec": {
      "*": {
        "*": "ONE"
      }
    }
  },
  {
    // get rid of object labels
    "operation": "shift",
    "spec": {
      "*": "[]"
    }
  }
]
 类似资料:
  • 输入 json : 预期输出: 我想有一个颠簸转换,它可以嵌套很少的田地。

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

  • 我有一个关于使用jolt将平面json转换成嵌套json的问题。我对jolt很陌生,这是我的意见 我编写了jolt spec,但我没有得到想要的输出 我的预期产出是: 任何震动专家都可以帮助我获得所需的输出。我应该在颠簸中使用多个变换,还是可以在一个震动变压器中获得所需的输出?

  • 我遇到了一个问题,使用颠簸转换将平面 JSON 转换为嵌套 JSON。而且我对颠簸转型很陌生。输入和输出详细信息如下。 我的输入: 预期产量

  • 我有一个JSON如下所示: 为什么在输出中看不到Level1、Level2?请有人帮忙,我想看看在输出和输入太相似了。

  • 我正在尝试编写一个规范来使用jolt转换进行以下转换。我需要通过保持空值将平面JSON转换为嵌套JSON。我附加了输入、预期输出和jolt转换。我需要在输出中保留空值,但在jolt转换后不会显示在输出中。我的jolt转换没有得到确切的输出。 我在将平面JSON转换为嵌套JSON时遇到了一些问题。我看了一些例子,但没有更进一步了解上面提到的内容。我需要通过使用JOLT规范来转换JSON结构。我用ht