如何修改Google Script从JSON中提取多个字段(API)

cbeh67ev  于 2023-01-22  发布在  Go
关注(0)|答案(1)|浏览(87)

我使用这个脚本从多个数组中提取特定的字段:

var values = json.data.flatMap(({ productId, cannabinoidInformation, weightTierInformation }) => cannabinoidInformation.map(({ lowerRange, name }, i) => [productId, lowerRange, name, weightTierInformation[i].name, weightTierInformation[i].gramAmount]));

对于此API:https://flowhub.stoplight.io/docs/public-developer-portal/a377fa23cdec5-inventory-items-by-location
我把剧本修改成:

var values = json.orders.flatMap(({ name, payments,itemsInCart }) => payments.map(({ amount,
paymentType }, i) => [name,amount,
paymentType,itemsInCart[i].quantity, itemsInCart[i].sku]));

我正在尝试使用以下API使其工作:https://flowhub.stoplight.io/docs/public-developer-portal/b41c3d4f8d234-orders-by-location
但我得到这个错误:错误类型错误:无法读取未定义的属性(读取"quantity")
我如何修改上面的脚本来修复我收到的错误消息?
不幸的是,模拟服务器不适用于上面的API,而且由于隐私原因,我不能共享密钥
下面是一个响应示例:

{
  "total": 0,
  "orders": [
    {
      "id": "string",
      "clientId": "string",
      "createdAt": "2019-08-24T14:15:22Z",
      "completedOn": "2019-08-24T14:15:22Z",
      "customerId": "string",
      "currentPoints": 0,
      "Name": "string",
      "orderStatus": "Pending",
      "orderType": "in-store",
      "orderId": "string",
      "totals": {
        "FinalTotal": 0,
        "SubTotal": 0,
        "TotalDiscounts": 0,
        "TotalFees": 0,
        "TotalTaxes": 0
      },
      "itemsInCart": [
        {
          "id": "string",
          "sku": "string",
          "category": "BulkBud",
          "title1": "string",
          "title2": "string",
          "productName": "string",
          "strainName": "string",
          "unitOfWeight": "Each",
          "quantity": 0,
          "unitPrice": 0,
          "totalPrice": 0,
          "unitCost": 0,
          "totalCost": 0,
          "itemDiscounts": [
            {
              "_id": "string",
              "name": "string",
              "type": "string",
              "discountAmount": 0,
              "discountType": "string",
              "discountId": "string",
              "dollarsOff": 0,
              "penniesOff": 0,
              "percentOff": 0,
              "discounterName": "string",
              "discounterId": "string",
              "isCartDiscount": true,
              "couponCode": "string",
              "quantity": 0
            }
          ],
          "tax": [
            {
              "_id": "string",
              "name": "string",
              "percentage": 0,
              "calculateBeforeDiscounts": "string",
              "supplierSpecificTax": true,
              "excludeCustomerGroups": [
                "string"
              ],
              "enableCostMarkup": true,
              "markupPercentage": 0,
              "thisTaxInPennies": 0,
              "appliesTo": "all"
            }
          ]
        }
      ],
      "customerType": "recCustomer",
      "locationId": "string",
      "voided": true,
      "fullName": "string",
      "budtender": "string",
      "payments": [
        {
          "_id": "string",
          "paymentType": "cash",
          "amount": 0,
          "cardId": "string",
          "loyaltyPoints": 0,
          "debitProvider": "string",
          "balanceAfterPayment": 0
        }
      ]
    }
  ]
}

我想提取以下字段:
1.姓名
1.付款〉付款
1.付款〉付款类型
1.商品购物车〉数量
1.商品购物车〉SKU

t40tm48m

t40tm48m1#

你需要做的是将paymentsitemsInCartMap到包含你需要的信息的更小的数组中。提取name很简单。试试下面的代码片段,让我知道它是否适合你,或者你是否需要我修改一些东西。

const apiResult = {
  total: 0,
  orders: [
    {
      id: 'string',
      clientId: 'string',
      createdAt: '2019-08-24T14:15:22Z',
      completedOn: '2019-08-24T14:15:22Z',
      customerId: 'string',
      currentPoints: 0,
      Name: 'string',
      orderStatus: 'Pending',
      orderType: 'in-store',
      orderId: 'string',
      totals: {
        FinalTotal: 0,
        SubTotal: 0,
        TotalDiscounts: 0,
        TotalFees: 0,
        TotalTaxes: 0,
      },
      itemsInCart: [
        {
          id: 'string',
          sku: 'string',
          category: 'BulkBud',
          title1: 'string',
          title2: 'string',
          productName: 'string',
          strainName: 'string',
          unitOfWeight: 'Each',
          quantity: 0,
          unitPrice: 0,
          totalPrice: 0,
          unitCost: 0,
          totalCost: 0,
          itemDiscounts: [
            {
              _id: 'string',
              name: 'string',
              type: 'string',
              discountAmount: 0,
              discountType: 'string',
              discountId: 'string',
              dollarsOff: 0,
              penniesOff: 0,
              percentOff: 0,
              discounterName: 'string',
              discounterId: 'string',
              isCartDiscount: true,
              couponCode: 'string',
              quantity: 0,
            },
          ],
          tax: [
            {
              _id: 'string',
              name: 'string',
              percentage: 0,
              calculateBeforeDiscounts: 'string',
              supplierSpecificTax: true,
              excludeCustomerGroups: ['string'],
              enableCostMarkup: true,
              markupPercentage: 0,
              thisTaxInPennies: 0,
              appliesTo: 'all',
            },
          ],
        },
      ],
      customerType: 'recCustomer',
      locationId: 'string',
      voided: true,
      fullName: 'string',
      budtender: 'string',
      payments: [
        {
          _id: 'string',
          paymentType: 'cash',
          amount: 0,
          cardId: 'string',
          loyaltyPoints: 0,
          debitProvider: 'string',
          balanceAfterPayment: 0,
        },
      ],
    },
  ],
};

(function () {
  const {orders}= apiResult;
  const result = orders.reduce((acc, order) => {
    const {Name: name, payments: _payments, itemsInCart: _itemsInCart } = order;
    const payments = _payments.map(payment => ({amount: payment.amount, type: payment.paymentType}))
    const itemsInCart = _itemsInCart.map(item => ({ quantity: item.quantity, sku: item.sku }))
    return [...acc, { name, payments, itemsInCart }]
  }, []);
  console.log(result)
})();

相关问题