簡體   English   中英

用Jolt進行Json頂層數組轉換

[英]Json top-level array transformation with Jolt

我正在嘗試使用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"
          }
        }
      }
    }
  }
]

這個規范做到了

[
  {
    "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"
          }
        }
      }
    }
  }
]

你近了 “客戶”對象的處理已關閉。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM