2017-04-24 79 views
0

我正在尝试使用Jolt来执行从顶层json数组到另一个数组的复杂转换。在每个项目中,我有几个对象需要映射到另一个。当它是简单的字符串时,我可以很容易地映射它们,但是当它是关于对象到对象的时候,我找不到如何去做。 在我下面的示例中,我想从客户对象中提取数据。我想这样做了几个人,好像地址ORDER_ITEMS 这是我输入:Json顶层数组转换

[ 
    { 
     "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": "[email protected]", 
       "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": "[email protected]", 
       "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": "[email protected]", 
      "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": "[email protected]", 
       "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": "[email protected]", 
       "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": "[email protected]", 
      "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" 
     } 
    } 
] 

这里是spec文件

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

有没有人有关于如何做到这一点的线索?

UPDATE: 如何我也改变我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" 
} ] 

SOLUTION:我自己想通了,这里是解决方案

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

回答

3

该规范做它

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

你接近。 “客户”对象的处理已关闭。

+0

嗨米洛,刚刚尝试过,并且对你的规格进行了小改动,效果很好。我必须删除_orders_键,因为我有一个顶级数组。非常感谢 ! –