用Jolt进行Json顶级数组转换

时间:2017-04-24 08:10:50

标签: json jolt

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

print("This really makes me sad!")

这是spec文件

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

有没有人知道怎么做?

更新 我怎样才能将 order_items 数组转换为另一个数组? 输入相同,但输出为:

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

解决方案:我自己想出来,这是解决方案

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

1 个答案:

答案 0 :(得分:4)

此规范是否

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

你很亲密。 “客户”对象的处理已关闭。