0
votes

I can't seem to extract the nested data I'm getting from a Shopify orders/create webhook and write it to a spreadsheet.

I was able to go down one level by using shift() like this var lineItems = myData.line_items.shift(); but the next level comes back as [Ljava.lang.Object;@1746c8f9

Though the first level of the object does come back readable.

Here is my code:

function doPost(e) {

  var myData = JSON.parse(e.postData.contents);

  var lineItems = myData.line_items.shift();
  var prop = lineItems.properties;

  var sheet = SpreadsheetApp.getActiveSheet();

  sheet.appendRow([lineItems,prop]); 
}

Here is how Shopify documentation says the webhook response will be: (I took out the irrelevant data to shorten)

I need to extract the line_items data and specifically the properties object (though in this example it is blank thats because it reflects custom properties entered by user)

{
  "id": 820982911946154508,
  "email": "[email protected]",
 
  "order_number": 1234,
  
  "order_status_url": "https:\/\/apple.myshopify.com\/690933842\/orders\/123456abcd\/authenticate?key=abcdefg",

  "line_items": [
    {
      "id": 866550311766439020,
      "variant_id": 808950810,
      "title": "IPod Nano - 8GB",
      "quantity": 1,
      "sku": "IPOD2008PINK",
      "variant_title": null,
      "vendor": null,
      "fulfillment_service": "manual",
      "product_id": 632910392,
      "requires_shipping": true,
      "taxable": true,
      "gift_card": false,
      "name": "IPod Nano - 8GB",
      "variant_inventory_management": "shopify",
      "properties": [
      ],
      "product_exists": true,
      "fulfillable_quantity": 1,
      "grams": 567,
      "price": "199.00",
      "total_discount": "0.00",
      "fulfillment_status": null,
      "price_set": {
        "shop_money": {
          "amount": "199.00",
          "currency_code": "USD"
        },
        "presentment_money": {
          "amount": "199.00",
          "currency_code": "USD"
        }
      },
      "total_discount_set": {
        "shop_money": {
          "amount": "0.00",
          "currency_code": "USD"
        },
        "presentment_money": {
          "amount": "0.00",
          "currency_code": "USD"
        }
      },
      "discount_allocations": [
      ],
      "duties": [
      ],
      "admin_graphql_api_id": "gid:\/\/shopify\/LineItem\/866550311766439020",
      "tax_lines": [
      ]
    },
    {
      "id": 141249953214522974,
      "variant_id": 808950810,
      "title": "IPod Nano - 8GB",
      "quantity": 1,
      "sku": "IPOD2008PINK",
      "variant_title": null,
      "vendor": null,
      "fulfillment_service": "manual",
      "product_id": 632910392,
      "requires_shipping": true,
      "taxable": true,
      "gift_card": false,
      "name": "IPod Nano - 8GB",
      "variant_inventory_management": "shopify",
      "properties": [
      ],
      "product_exists": true,
      "fulfillable_quantity": 1,
      "grams": 567,
      "price": "199.00",
      "total_discount": "5.00",
      "fulfillment_status": null,
      "price_set": {
        "shop_money": {
          "amount": "199.00",
          "currency_code": "USD"
        },
        "presentment_money": {
          "amount": "199.00",
          "currency_code": "USD"
        }
      },
      "total_discount_set": {
        "shop_money": {
          "amount": "5.00",
          "currency_code": "USD"
        },
        "presentment_money": {
          "amount": "5.00",
          "currency_code": "USD"
        }
      },
      "discount_allocations": [
        {
          "amount": "5.00",
          "discount_application_index": 0,
          "amount_set": {
            "shop_money": {
              "amount": "5.00",
              "currency_code": "USD"
            },
            "presentment_money": {
              "amount": "5.00",
              "currency_code": "USD"
            }
          }
        }
      ],
      "duties": [
      ],
      "admin_graphql_api_id": "gid:\/\/shopify\/LineItem\/141249953214522974",
      "tax_lines": [
      ]
    }
  ],
  "total_tip_received": "0.0",
  "original_total_duties_set": null,
2

2 Answers

0
votes

Convert prop to a string using whatever method you prefer.

sheet.appendRow([lineItems,JSON.stringify(prop)]);

You may also want to stringify lineItems.

0
votes

I was able to figure how to access the properties. If someone has a better way please answer too.

Also please see full code in this question: Quicken google apps script so I can return success response within Shopify 5 second limit

  var data = JSON.parse(e.postData.contents);
 
  var l = data.line_items.length; 
 
  for (var i=0;i<l;i++){
  var prop = data.line_items[i].properties;

  var pdf = prop.find(function(x) {if(x.name == "_pdf") return x});
  if (!pdf){pdf = "Prop not found";}else{pdf = pdf.value};