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

使用 Jolt 进行 Json 顶级阵列转换

赏新知
2023-03-14

我正在尝试使用Jolt进行从顶级json数组到另一个数组的复杂转换。在每个项目中,我都有几个需要映射到另一个的对象。当它是简单的字符串时,我可以很容易地映射它们,但当它是关于对象到对象的时,我无法找到如何进行映射。在下面的示例中,我想从客户对象中提取数据。我想为其他几个人做这件事,比如地址和order_items这是我的输入:

[
    {
        "tax_amount": "0.0000",
        "shipping_discount_amount": "0.0000",
        "addresses": [
            {
                "firstname": "kevin",
                "address_type": "billing",
                "city": "PARIS",
                "prefix": null,
                "postcode": "75013",
                "middlename": null,
                "telephone": "+33620350072",
                "suffix": null,
                "lastname": "test",
                "street": "18 Rue Wurtz",
                "company": null,
                "region": "Paris",
                "email": "kevintest@gmail.com",
                "country_id": "FR"
            },
            {
                "firstname": "kevin",
                "address_type": "shipping",
                "city": "PARIS",
                "prefix": null,
                "postcode": "75013",
                "middlename": null,
                "telephone": "+33620350072",
                "suffix": null,
                "lastname": "test",
                "street": "18 Rue Wurtz",
                "company": null,
                "region": "Paris",
                "email": "kevintest@gmail.com",
                "country_id": "FR"
            }
        ],
        "discount_amount": "0.0000",
        "store_currency_code": "EUR",
        "created_at": "2014-11-12 16:40:58",
        "base_total_refunded": null,
        "gift_message_body": null,
        "tax_rate": null,
        "shipping_tax_amount": "0.0000",
        "remote_ip": "109.190.111.143",
        "total_paid": null,
        "total_refunded": null,
        "base_shipping_discount_amount": "0.0000",
        "increment_id": "200000002",
        "order_comments": [
            {
                "is_visible_on_front": "0",
                "created_at": "2014-11-12 16:40:58",
                "comment": null,
                "is_customer_notified": "1",
                "status": "pending"
            },
            {
                "is_visible_on_front": "0",
                "created_at": "2014-11-12 16:41:17",
                "comment": null,
                "is_customer_notified": "2",
                "status": "canceled"
            },
            {
                "is_visible_on_front": "0",
                "created_at": "2014-11-22 10:47:27",
                "comment": "test",
                "is_customer_notified": "1",
                "status": "canceled"
            }
        ],
        "gift_message_from": null,
        "discount_description": null,
        "store_name": "centrale\ncentrale\npharmacie-centrale",
        "grand_total": "5.0000",
        "base_currency_code": "EUR",
        "base_total_paid": null,
        "payment_method": "checkmo",
        "base_shipping_tax_amount": "0.0000",
        "base_tax_amount": "0.0000",
        "order_items": [
            {
                "tax_amount": "0.0000",
                "original_price": "0.0000",
                "item_id": "1",
                "discount_amount": "0.0000",
                "qty_shipped": "0.0000",
                "base_discount_amount": "0.0000",
                "qty_ordered": "1.0000",
                "base_original_price": "0.0000",
                "row_total": "0.0000",
                "qty_canceled": "1.0000",
                "tax_percent": "0.0000",
                "base_row_total_incl_tax": "0.0000",
                "base_price_incl_tax": "0.0000",
                "qty_refunded": "0.0000",
                "parent_item_id": null,
                "price_incl_tax": "0.0000",
                "price": "0.0000",
                "name": "L HOMME SPORT DEODORANT ROLL ON",
                "qty_invoiced": "0.0000",
                "base_price": "0.0000",
                "base_row_total": "0.0000",
                "sku": "3337875200387",
                "row_total_incl_tax": "0.0000",
                "base_tax_amount": "0.0000"
            }
        ],
        "base_grand_total": "5.0000",
        "coupon_code": null,
        "total_due": "5.0000",
        "tax_name": null,
        "gift_message_to": null,
        "base_discount_amount": "0.0000",
        "entity_id": "1",
        "shipping_description": "Flat Rate - Fixed",
        "store_to_order_rate": "1.0000",
        "shipping_amount": "5.0000",
        "base_shipping_amount": "5.0000",
        "subtotal_incl_tax": "0.0000",
        "subtotal": "0.0000",
        "base_subtotal": "0.0000",
        "base_total_due": "5.0000",
        "customer_id": "1",
        "base_subtotal_incl_tax": "0.0000",
        "shipping_incl_tax": "5.0000",
        "status": "canceled",
        "customer": {
            "entity_id": "1",
            "website_id": "2",
            "email": "kevintest@gmail.com",
            "group_id": "1",
            "created_at": "2014-11-12T17:39:42+01:00",
            "disable_auto_group_change": "0",
            "created_in": "pharmacie-centrale",
            "firstname": "kevin",
            "lastname": "test",
            "gender": "1",
            "dob": "1980-11-03 00:00:00",
            "last_logged_in": "2015-10-07 10:22:38"
        }
    },
    {
        "tax_amount": "0.0000",
        "shipping_discount_amount": "0.0000",
        "addresses": [
            {
                "firstname": "kevin",
                "address_type": "billing",
                "city": "PARIS",
                "prefix": null,
                "postcode": "75013",
                "middlename": null,
                "telephone": "+33620350072",
                "suffix": null,
                "lastname": "test",
                "street": "18 Rue Wurtz",
                "company": null,
                "region": "Paris",
                "email": "kevintest@gmail.com",
                "country_id": "FR"
            },
            {
                "firstname": "kevin",
                "address_type": "shipping",
                "city": "PARIS",
                "prefix": null,
                "postcode": "75013",
                "middlename": null,
                "telephone": "+33620350072",
                "suffix": null,
                "lastname": "test",
                "street": "18 Rue Wurtz",
                "company": null,
                "region": "Paris",
                "email": "kevintest@gmail.com",
                "country_id": "FR"
            }
        ],
        "discount_amount": "0.0000",
        "store_currency_code": "EUR",
        "created_at": "2015-01-13 11:45:26",
        "base_total_refunded": null,
        "gift_message_body": null,
        "tax_rate": null,
        "shipping_tax_amount": "0.0000",
        "remote_ip": "109.190.111.143",
        "total_paid": null,
        "total_refunded": null,
        "base_shipping_discount_amount": "0.0000",
        "increment_id": "200000003",
        "order_comments": [
            {
                "is_visible_on_front": "0",
                "created_at": "2015-01-13 11:45:26",
                "comment": null,
                "is_customer_notified": "1",
                "status": "pending"
            },
            {
                "is_visible_on_front": "0",
                "created_at": "2015-01-13 11:47:24",
                "comment": null,
                "is_customer_notified": "2",
                "status": "canceled"
            }
        ],
        "gift_message_from": null,
        "discount_description": null,
        "store_name": "centrale\ncentrale\npharmacie-centrale",
        "grand_total": "5.0000",
        "base_currency_code": "EUR",
        "base_total_paid": null,
        "payment_method": "ccsave",
        "base_shipping_tax_amount": "0.0000",
        "base_tax_amount": "0.0000",
        "order_items": [
            {
                "tax_amount": "0.0000",
                "original_price": "0.0000",
                "item_id": "2",
                "discount_amount": "0.0000",
                "qty_shipped": "0.0000",
                "base_discount_amount": "0.0000",
                "qty_ordered": "1.0000",
                "base_original_price": "0.0000",
                "row_total": "0.0000",
                "qty_canceled": "1.0000",
                "tax_percent": "0.0000",
                "base_row_total_incl_tax": "0.0000",
                "base_price_incl_tax": "0.0000",
                "qty_refunded": "0.0000",
                "parent_item_id": null,
                "price_incl_tax": "0.0000",
                "price": "0.0000",
                "name": "LOT*2 DEODORANT BILLE  ANTI TRANSPIRANT",
                "qty_invoiced": "0.0000",
                "base_price": "0.0000",
                "base_row_total": "0.0000",
                "sku": "3433425000640",
                "row_total_incl_tax": "0.0000",
                "base_tax_amount": "0.0000"
            }
        ],
        "base_grand_total": "5.0000",
        "coupon_code": null,
        "total_due": "5.0000",
        "tax_name": null,
        "gift_message_to": null,
        "base_discount_amount": "0.0000",
        "entity_id": "2",
        "shipping_description": "Flat Rate - Fixed",
        "store_to_order_rate": "1.0000",
        "shipping_amount": "5.0000",
        "base_shipping_amount": "5.0000",
        "subtotal_incl_tax": "0.0000",
        "subtotal": "0.0000",
        "base_subtotal": "0.0000",
        "base_total_due": "5.0000",
        "customer_id": "1",
        "base_subtotal_incl_tax": "0.0000",
        "shipping_incl_tax": "5.0000",
        "status": "canceled",
        "customer": {
            "entity_id": "1",
            "website_id": "2",
            "email": "kevintest@gmail.com",
            "group_id": "1",
            "created_at": "2014-11-12T17:39:42+01:00",
            "disable_auto_group_change": "0",
            "created_in": "pharmacie-centrale",
            "firstname": "kevin",
            "lastname": "test",
            "gender": "1",
            "dob": "1980-11-03 00:00:00",
            "last_logged_in": "2015-10-07 10:22:38"
        }
    }
]

这是规格文件

[
  {
    "operation": "shift",
    "spec": {
      "orders": {
        "*": {
          "customer": {
            "entity_id": "customer[&1].id",
            "firstname": "customer[&1].firstName",
            "lastname": "customer[&1].lastName",
            "dob": "customer[&1].birthDate"
          },
          "increment_id": "[&1].saleId",
          "created_at": "[&1].saleDate",
          "shipping_amount": "[&1].shippingFeesWithoutTax",
          "shipping_tax_amount": "[&1].shippingFeesVTA",
          "grand_total": "[&1].totalAmount"
        }
      }
    }
  }
]

目前,我的输出是

[ {
  "saleDate" : "2014-11-12 16:40:58",
  "saleId" : "200000002",
  "shippingFeesVTA" : "0.0000",
  "shippingFeesWithoutTax" : "5.0000",
  "totalAmount" : "5.0000"
}, {
  "saleDate" : "2015-01-13 11:45:26",
  "saleId" : "200000003",
  "shippingFeesVTA" : "0.0000",
  "shippingFeesWithoutTax" : "5.0000",
  "totalAmount" : "5.0000"
} ]

我希望这样:

[ {
  "saleDate" : "2014-11-12 16:40:58",
  "saleId" : "200000002",
  "shippingFeesVTA" : "0.0000",
  "shippingFeesWithoutTax" : "5.0000",
  "totalAmount" : "5.0000",
  "customer": {
    "id": 1,
    "firstName": "kevin",
    "lastName":"test",
    "birthDate":"1980-11-03 00:00:00"
  }
}, {
  "saleDate" : "2015-01-13 11:45:26",
  "saleId" : "200000003",
  "shippingFeesVTA" : "0.0000",
  "shippingFeesWithoutTax" : "5.0000",
  "totalAmount" : "5.0000",
  "customer": {
    "id": 1,
    "firstName": "kevin",
    "lastName":"test",
    "birthDate":"1980-11-03 00:00:00"
  }
} ]

有人知道怎么做吗?

更新:如何将我的order_items数组转换为另一个阵列?输入相同,但输出为:

    [ {
  "customer" : {
    "birthDate" : "1980-11-03 00:00:00",
    "firstName" : "kevin",
    "id" : "1",
    "lastName" : "test"
  },
  "details" : [ {
    "VTA" : "0.0000",
    "detailsId" : "1",
    "discount" : "0.0000",
    "grossPrice" : "0.0000",
    "price" : "0.0000",
    "productCode" : "3337875200387",
    "productName" : "L HOMME SPORT DEODORANT ROLL ON",
    "quantity" : "1.0000"
  }, {
    "VTA" : "0.0000",
    "detailsId" : "1",
    "discount" : "0.0000",
    "grossPrice" : "0.0000",
    "price" : "0.0000",
    "productCode" : "3337875200387",
    "productName" : "L HOMME SPORT DEODORANT ROLL ON",
    "quantity" : "1.0000"
  } ],
  "saleDate" : "2014-11-12 16:40:58",
  "saleId" : "200000002",
  "shippingFeesVTA" : "0.0000",
  "shippingFeesWithoutTax" : "5.0000",
  "totalAmount" : "5.0000"
}, {
  "customer" : {
    "birthDate" : "1980-11-03 00:00:00",
    "firstName" : "kevin",
    "id" : "1",
    "lastName" : "test"
  },
  "details" : [ {
    "VTA" : "0.0000",
    "detailsId" : "2",
    "discount" : "0.0000",
    "grossPrice" : "0.0000",
    "price" : "0.0000",
    "productCode" : "3433425000640",
    "productName" : "LOT*2 DEODORANT BILLE  ANTI TRANSPIRANT",
    "quantity" : "1.0000"
  } ],
  "saleDate" : "2015-01-13 11:45:26",
  "saleId" : "200000003",
  "shippingFeesVTA" : "0.0000",
  "shippingFeesWithoutTax" : "5.0000",
  "totalAmount" : "5.0000"
} ]

解决方法:我自己想出来的,下面是解决方法

[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "increment_id": "[&1].saleId",
        "created_at": "[&1].saleDate",
        "shipping_amount": "[&1].shippingFeesWithoutTax",
        "shipping_tax_amount": "[&1].shippingFeesVTA",
        "grand_total": "[&1].totalAmount",
        "customer": {
          "entity_id": "[&2].customer.id",
          "firstname": "[&2].customer.firstName",
          "lastname": "[&2].customer.lastName",
          "dob": "[&2].customer.birthDate"
        },
        "order_items": {
          "*": {
            "item_id": "[&3].details[&1].detailsId",
            "sku": "[&3].details[&1].productCode",
            "name": "[&3].details[&1].productName",
            "qty_ordered": "[&3].details[&1].quantity",
            "row_total": "[&3].details[&1].grossPrice",
            "discount_amount": "[&3].details[&1].discount",
            "price_incl_tax": "[&3].details[&1].price",
            "tax_percent": "[&3].details[&1].VTA"
          }
        }
      }
    }
  }
]

共有1个答案

史宸
2023-03-14

这个规格可以做到

[
  {
    "operation": "shift",
    "spec": {
      "orders": {
        "*": {
          "increment_id": "[&1].saleId",
          "created_at": "[&1].saleDate",
          "shipping_amount": "[&1].shippingFeesWithoutTax",
          "shipping_tax_amount": "[&1].shippingFeesVTA",
          "grand_total": "[&1].totalAmount",

          "customer": {
            "entity_id": "[&2].customer.id",
            "firstname": "[&2].customer.firstName",
            "lastname": "[&2].customer.lastName",
            "dob": "[&2].customer.birthDate"
          }
        }
      }
    }
  }
]

你离得很近。“客户”对象的处理已关闭。

 类似资料:
  • 我正在尝试使用JOLT(使用NiFi JoltTransformJson处理器)将JSON转换为不同的格式。对于单个JSON记录,正在使用的JOLT在JOLT应用程序演示中运行良好,而如果我使用多个JSON记录执行,那么我在JOLT应用程序演示中没有得到预期的输出。有人能告诉我在JOLT规范中需要做哪些额外的更改来处理多个JSON记录吗? 示例输入json JOLT使用: 预期输出JSON:

  • 我正在尝试使用 Jolt 从一个 JSON 数组转换为另一个数组。它由一个没有键的嵌套 JSON 数组组成。 这是我的意见: 我想得到以下输出: 我能够使用此规范文件为单个嵌套数组元素添加键: 但我不知道如何将此应用于外部JSON数组。

  • 我需要在颠簸转换规范方面的帮助。以下是我到目前为止的工作。 输入: 使用的震动代码: 电流输出: 预期产出 当只使用单个json对象时,此代码工作正常。但是当我们使用具有相同id的多个项目时,它会开始对所有相关字段进行分组。

  • 我目前有点卡住了,我需要使用JOLT转换JSON,但就我而言,我无法获得与我一起工作的数据/结构。 我有以下需要转换的数据集: 预期的结果应该是这样的: 我可能在我想要的结果中犯了一个错误,但基本上,“riskItemAllRisk”部分需要在一个数组中。 我正在进行的JOLT转换是: 为可怕的代码道歉,但这对我来说是第一次。 谢谢。

  • `我有以下输入json格式,需要转换以下json文件。我正在使用jolt转换,但无法使用https://jolt-demo.appspot.com/#inception网站正确格式化输出 `需要以下使用JOLT Iam的输出json格式,尝试使用JOLT转换进行转换

  • 我想转换这个JSON: 对此JSON: 我目前正在使用该规范,但它不适合我: 有人能给出一个规范吗?有没有关于jolt JSON的明确文档 ................................................................................................................................