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

使用多个数组将平面json转换为嵌套Json,并使用Jolt转换在输出中保留空值

欧桐
2023-03-14

我正在尝试编写一个规范来使用jolt转换进行以下转换。我需要通过保持空值将平面JSON转换为嵌套JSON。我附加了输入、预期输出和jolt转换。我需要在输出中保留空值,但在jolt转换后不会显示在输出中。我的jolt转换没有得到确切的输出。

我在将平面JSON转换为嵌套JSON时遇到了一些问题。我看了一些例子,但没有更进一步了解上面提到的内容。我需要通过使用JOLT规范来转换JSON结构。我用https://jolt-demo.appspot.com来测试以下内容。

输入:

[
  {
    "container_id": "ABC",
    "shipperN": null,
    "PNumber": null,
    "trackingNumber": null,
    "priority": null,
    "HType": "IN_Load",
    "loadNumber": "123345",
    "billOfLading": "12345",
    "referenceNumbers": "LID",
    "addressLine1": "ABC Street",
    "addressLine2": "null",
    "city": "Chicago",
    "country": "US",
    "latitude": "null",
    "longitude": "null",
    "earliestAppointmentTime": "XXXXX09:25",
    "latestAppointmentTime": "XXXXX09:25",
    "postalCode": "XXXXX3",
    "sequence": "1",
    "state": "XY",
    "stopReferenceId": "0001",
    "stopType": "PU",
    "truckNumber": null,
    "trailerNumber": null,
    "driverPhone": null,
    "railEquipmentInitials": null,
    "railEquipmentNumber": null,
    "containerNumber": "XXXXXXXX"
  },
  {
    "container_id": "ABC",
    "shipperN": null,
    "PNumber": null,
    "trackingNumber": null,
    "priority": null,
    "HType": "IN_Load",
    "loadNumber": "123345",
    "billOfLading": "12345",
    "referenceNumbers": "LID",
    "addressLine1": "null",
    "addressLine2": "null",
    "city": "null",
    "country": "null",
    "latitude": null,
    "longitude": null,
    "earliestAppointmentTime": "XXXXX09:25",
    "latestAppointmentTime": "XXXXX09:25",
    "name": "null",
    "postalCode": "null",
    "sequence": "2",
    "state": "null",
    "stopReferenceId": "XXXXD",
    "stopType": "PL",
    "truckNumber": null,
    "trailerNumber": null,
    "driverPhone": null,
    "railEquipmentInitials": null,
    "railEquipmentNumber": null,
    "containerNumber": "XXXXXXXX"
  }
]

期望输出:

{
  "load": {
    "container_id": "ABC",
    "shipperN": null,
    "PNumber": null,
    "trackingNumber": null,
    "priority": null,
    "HType": [ "IN_Load" ],
    "loadNumber": "123345",
    "billOfLading": "12345",
    "referenceNumbers": [ "LID" ],
    "stops": [
      {
        "addressLine1": "ABC Street",
        "addressLine2": "null",
        "city": "Chicago",
        "country": "US",
        "earliestAppointmentTime": "XXXXX09:25",
        "latestAppointmentTime": "XXXXX09:25",
        "postalCode": "XXXXX3",
        "sequence": "1",
        "state": "XY",
        "stopReferenceId": "0001",
        "stopType": "PU"
      },
      {
        "earliestAppointmentTime": "2021-03-09T15:25:00.203Z",
        "latestAppointmentTime": "2021-03-09T15:25:00.203Z",
        "sequence": "2",
        "stopReferenceId": "dummy",
        "stopType": "PL",
        "externalAddressId": "dummy"
      }
    ]
  },
  "containerInfo": {
    "containerNumber": "XXXXXXXX"
  },
  "trackingInfo": {
    "truckNumber": null,
    "trailerNumber": null,
    "driverPhone": null,
    "railEquipmentInitials": null,
    "railEquipmentNumber": null
  }
}

我使用的Jolt Spec:

[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "*": "@(1,container_id).load.stops[&1].&",
        "container_id": "@(1,container_id).load.&", // "else" case
        "shipperN": "@(1,container_id).load.&",
        "PNumber": "@(1,container_id).load.&",
        "trackingNumber": "@(1,container_id).load.&",
        "priority": "@(1,container_id).load.&",
        "HType": "@(1,container_id).load.&",
        "loadNumber": "@(1,container_id).load.&",
        "billOfLading": "@(1,container_id).load.&",
        "referenceNumbers": "@(1,container_id).load.&",
        "containerNumber": "@(1,container_id).containerInfo.&",
        "truckNumber": "@(1,container_id).trackingInfo.&",
        "trailerNumber": "@(1,container_id).trackingInfo.&",
        "driverPhone": "@(1,container_id).trackingInfo.&",
        "railEquipmentInitials": "@(1,container_id).trackingInfo.&",
        "railEquipmentNumber": "@(1,container_id).trackingInfo.&"
      }
    }
  },
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "*": "=recursivelySquashNulls"
    }
  },
  {
    "operation": "cardinality",
    "spec": {
      "*": {
        "*": {
          "container_id": "ONE",
          "shipperN": "ONE",
          "PNumber": "ONE",
          "trackingNumber": "ONE",
          "priority": "ONE",
          "HType": "ONE",
          "referenceNumbers": "ONE",
          "loadNumber": "ONE",
          "billOfLading": "ONE",
          "containerInfo": {
            "*": "ONE"
          },
          "trackingInfo": {
            "*": "ONE"
          }
        }
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "*": ""
    }
  }
]

共有1个答案

孟哲
2023-03-14

你离得太近了;

>

  • 包含的规范应被删除

    标识符

    基数规格最好缩短

    因此,使用以下作为一个整体规范

    [
      {
        "operation": "shift",
        "spec": {
          "*": {
            "*": "@(1,container_id).load.stops[&1].&",
            "container_id": "@(1,container_id).load.&", // "else" case
            "shipperN": "@(1,container_id).load.&",
            "PNumber": "@(1,container_id).load.&",
            "trackingNumber": "@(1,container_id).load.&",
            "priority": "@(1,container_id).load.&",
            "HType": "@(1,container_id).load.&",
            "loadNumber": "@(1,container_id).load.&",
            "billOfLading": "@(1,container_id).load.&",
            "referenceNumbers": "@(1,container_id).load.&",
            "containerNumber": "@(1,container_id).containerInfo.&",
            "truckNumber": "@(1,container_id).trackingInfo.&",
            "trailerNumber": "@(1,container_id).trackingInfo.&",
            "driverPhone": "@(1,container_id).trackingInfo.&",
            "railEquipmentInitials": "@(1,container_id).trackingInfo.&",
            "railEquipmentNumber": "@(1,container_id).trackingInfo.&"
          }
        }
      },
      {
        "operation": "cardinality",
        "spec": {
          "*": {
            "*": {
              "*": "ONE",
              "stops": "MANY"
            }
          }
        }
      },
      {
        "operation": "shift",
        "spec": {
          "*": {
            "*": "&",
            "load": {
              "HType|referenceNumbers": "&1.&[]", 
              "*": "&1.&" // &1 stands for the key "load", and & replicates the leaf values  
            }
          }
        }
      }
    ]
    

  •  类似资料:
    • 输入 json : 预期输出: 我想有一个颠簸转换,它可以嵌套很少的田地。

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

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

    • 我试图写一个规范来使用jolt转换完成下面的转换。我需要将平面JSON转换成嵌套JSON。 输入数据: 我在将平面JSON转换为嵌套JSON时遇到了一些问题。这里,我希望基于stoptype属性聚合数据,并且需要针对唯一的有效负载进行聚合。我用https://jolt-demo.appspot.com来测试以下内容。 输出: 你能帮我完成这个预期的输出吗?

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

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